一亩三分地论坛

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

扫码关注一亩三分地公众号
查看: 2114|回复: 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:
Given T, T lines follow. Each line contains
N - Number to be formed
Output Format
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. from: 1point3acres.com/bbs
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.. 1point 3acres 璁哄潧
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.
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
.1point3acres缃
Explanation
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就是好用!)
(ps. 发帖的时候选公司那里居然没有Twitter_(:з」∠)_)
NdrZmansN 发表于 2015-1-23 06:36:54 | 显示全部楼层
谢谢分享.
楼主方便分享一下第二题的解法么?
谢谢,
回复 支持 反对

使用道具 举报

haiken 发表于 2015-1-24 02:00:44 | 显示全部楼层
NdrZmansN 发表于 2015-1-23 06:36
谢谢分享.
楼主方便分享一下第二题的解法么?
谢谢,

主要是要写个最大公约数的func, 我想.
.1point3acres缃
写个了C++的版本, 不是很简洁, 最大公约数也可以用C里自带的__gcd(). 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

using Pair = pair<int, int>;

int _gcd(int a, int b){
        return b==0 ? a : _gcd(b, a%b);
}

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){
                gcd = _gcd(R.first, R.second);-google 1point3acres
                R.first = R.first/gcd;
                R.second = R.second/gcd;. from: 1point3acres.com/bbs
                int first = res.first * R.second + R.first  * res.second;
                int second = R.second * res.second;
. From 1point 3acres bbs                gcd = _gcd(first, second);
                res.first = first/gcd;
                res.second = second/gcd;
        }       
        return res;
}
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

#greatest common divider
def gcd(a,b):
    while a :. 1point 3acres 璁哄潧
        a,b = b%a,a
    return b. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

#lowest common multiplier
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 | 显示全部楼层
第二题
Constraints
1 <= N <= 15
1 <= ai <= 10
1 <= bi <= 10.
竟然这么小?那可不可以直接把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, 2016-12-5 20:37

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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