一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 2222|回复: 8
收起左侧

Twitter Online Assessment (HackerRank Challenge)

[复制链接] |试试Instant~ |关注本帖
pro 发表于 2014-11-5 13:21:44 | 显示全部楼层 |阅读模式

2014(10-12月) 码农类 硕士 全职@Twitter - 网上海投 - 在线笔试 |Pass

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
1. Two Operations (Programming)
You are given only two operations, ADD_1 and MULTIPLY_2. You start from 0 and using the two operations reach a number N. Find the least number of operations needed to do this.
Input Format: . visit 1point3acres.com for more.
Given T, T lines follow. Each line contains. from: 1point3acres.com/bbs
N - Number to be formed
Output Format. From 1point 3acres bbs
Print the minimum number of operations needed in each line corresponding to each test case.
Constraints
T <= 10000
N <= 1016
Sample Input
2
5
3
Sample Output
4
3
Explanation
Case1: To reach 5 from 0. We do ADD1 then Multiply2 two times and then add 1 again , total 4 steps
Case2: To reach 3 from 0. We can either do Add 1 3 times or Add 1 , multiply 2 and again add 1


2. Rational Sum (Programming)
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q , where p & q are two integers, and the denominator q is not equal to zero. Hence, all integers are rational numbers  where denominator, in the most reduced form, is equal to 1.
You are given a list of N rational number, {a1/b1, a2/b2, ..., aN/bN}. Print the sum ( = a1/b1 + a2/b2 + ... + aN/bN = num/den) in the most reduced form.. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
Input
The first line of input contains an integer, N, the number of rational numbers.  N lines follow. ithline contains two space separated integers, ai bi, where aiis the numerator and bi is the denominator for the ith rational number.
Output
You have to print two space separated integers, num den, where num and den are numerator and denominator of the sum respectively.-google 1point3acres
Constraints
  • 1 <= N <= 15
  • 1 <= ai <= 10
  • 1 <= bi <= 10
Notes
  • Make sure the sum displayed as output is in the most reduced form.
  • If sum is an integer, you have to print 1 as denominator.
Sample Input
4
4 2
2 4
2 4
2 3
Sample Output
11 3. visit 1point3acres.com for more.

Explanation. From 1point 3acres bbs
Sum is 4/2 + 2/4 + 2/4 + 2/3 = (24 + 6 + 6 + 8)/12 = 44/12 = 11/3. So you have to print "11 3", which is the most reduced form.

--------------------

有一点比较奇怪的就是,听说Twitter应该是做完笔试再通知phone interview的,但是我笔试都还没做phone interview的邮件就来了_(:з」∠)_

(ps. HackerRank就是好用!). 鍥磋鎴戜滑@1point 3 acres
(ps. 发帖的时候选公司那里居然没有Twitter_(:з」∠)_)
NdrZmansN 发表于 2015-1-23 06:36:54 | 显示全部楼层
谢谢分享..鏈枃鍘熷垱鑷1point3acres璁哄潧
楼主方便分享一下第二题的解法么?
谢谢,
回复 支持 反对

使用道具 举报

haiken 发表于 2015-1-24 02:00:44 | 显示全部楼层
NdrZmansN 发表于 2015-1-23 06:36
谢谢分享. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
楼主方便分享一下第二题的解法么?
谢谢,

主要是要写个最大公约数的func, 我想.
.1point3acres缃
写个了C++的版本, 不是很简洁, 最大公约数也可以用C里自带的__gcd()
. Waral 鍗氬鏈夋洿澶氭枃绔,
using Pair = pair<int, int>;

int _gcd(int a, int b){
        return b==0 ? a : _gcd(b, a%b);
}. 1point 3acres 璁哄潧

Pair rationalSum(vector<Pair> &R){
        int gcd = _gcd(R[0].first, R[0].second);
        Pair res = {Pair(R[0].first/gcd, R[0].second/gcd)};
        for(int i=1; i<R.size(); ++i){. 1point3acres.com/bbs
                gcd = _gcd(R.first, R.second);
                R.first = R.first/gcd;
                R.second = R.second/gcd;
                int first = res.first * R.second + R.first  * res.second;
                int second = R.second * res.second;
                gcd = _gcd(first, second);
                res.first = first/gcd;. Waral 鍗氬鏈夋洿澶氭枃绔,
                res.second = second/gcd;. 1point 3acres 璁哄潧
        }       
        return res;
}
回复 支持 反对

使用道具 举报

douya 发表于 2015-2-2 04:46:02 | 显示全部楼层
谢谢分享,这个是online coding challenge?
回复 支持 反对

使用道具 举报

tyr034 发表于 2015-3-1 13:31:04 | 显示全部楼层
def rationalSum(lst):
    return functools.reduce(add,lst). 鍥磋鎴戜滑@1point 3 acres

#greatest common divider
def gcd(a,b):.鏈枃鍘熷垱鑷1point3acres璁哄潧
    while a :
        a,b = b%a,a
    return b

#lowest common multiplier . 鍥磋鎴戜滑@1point 3 acres
def lcm(a,b):.鐣欏璁哄潧-涓浜-涓夊垎鍦
    return a*b//gcd(a,b)
      
def add(a,b):
   
    de = lcm(a[1],b[1])
    nu = a[0]*de//a[1] + b[0]*de//b[1]
    return(nu,de)
回复 支持 反对

使用道具 举报

manjusaka077 发表于 2015-3-11 04:22:21 | 显示全部楼层
第二题. more info on 1point3acres.com
Constraints
1 <= N <= 15
1 <= ai <= 10
1 <= bi <= 10.. more info on 1point3acres.com
竟然这么小?那可不可以直接把10以内的质数列出来放一个数组里,然后在分母里面找都出现过哪几个质数乘一下就是最终的分母了……
回复 支持 反对

使用道具 举报

nibuxing 发表于 2015-3-13 21:37:08 | 显示全部楼层
请问公司在Hackerrank上的test是不是都是考算法啊
回复 支持 反对

使用道具 举报

liuliuion1 发表于 2015-3-29 03:31:44 | 显示全部楼层
请问第一题该怎么做呢?
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2017-1-20 16:10

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表