May 2009 - May 2017 论坛八周年-你的足迹,我的骄傲


一亩三分地论坛

 找回密码
 获取更多干活,快来注册

一亩三分地官方iOS手机应用下载
查看: 2524|回复: 8
收起左侧

Twitter Online Assessment (HackerRank Challenge)

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

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

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

x
1. Two Operations (Programming). From 1point 3acres bbs
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
N - Number to be formed
Output Format
Print the minimum number of operations needed in each line corresponding to each test case.. 1point3acres.com/bbs
Constraints
T <= 10000
N <= 1016
Sample Input
2
5
. more info on 1point3acres.com3
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.. from: 1point3acres.com/bbs
Output.鏈枃鍘熷垱鑷1point3acres璁哄潧
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. From 1point 3acres bbs
2 4
2 3
Sample Output
11 3

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的邮件就来了_(:з」∠)_
.1point3acres缃
(ps. HackerRank就是好用!)
(ps. 发帖的时候选公司那里居然没有Twitter_(:з」∠)_)
NdrZmansN 发表于 2015-1-23 06:36:54 | 显示全部楼层
关注一亩三分地公众号:
Warald_一亩三分地
谢谢分享.. more info on 1point3acres.com
楼主方便分享一下第二题的解法么?
谢谢,
回复 支持 反对

使用道具 举报

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

主要是要写个最大公约数的func, 我想. . 1point3acres.com/bbs

写个了C++的版本, 不是很简洁, 最大公约数也可以用C里自带的__gcd()
. visit 1point3acres.com for more.
using Pair = pair<int, int>;

int _gcd(int a, int b){
        return b==0 ? a : _gcd(b, a%b);
}
. more info on 1point3acres.com
Pair rationalSum(vector<Pair> &R){.鏈枃鍘熷垱鑷1point3acres璁哄潧
        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);
                R.first = R.first/gcd;
                R.second = R.second/gcd;
                int first = res.first * R.second + R.first  * res.second;.鏈枃鍘熷垱鑷1point3acres璁哄潧
                int second = R.second * res.second;
                gcd = _gcd(first, second);
                res.first = first/gcd;
                res.second = second/gcd;
        }        . 鍥磋鎴戜滑@1point 3 acres
        return res;
}
回复 支持 反对

使用道具 举报

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

使用道具 举报

tyr034 发表于 2015-3-1 13:31:04 | 显示全部楼层
def rationalSum(lst):. from: 1point3acres.com/bbs
    return functools.reduce(add,lst)

#greatest common divider
def gcd(a,b):
    while a :
        a,b = b%a,a
    return b
. 鍥磋鎴戜滑@1point 3 acres
#lowest common multiplier
def lcm(a,b):
    return a*b//gcd(a,b). 鍥磋鎴戜滑@1point 3 acres
      
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. visit 1point3acres.com for more.
1 <= ai <= 10
1 <= bi <= 10.
竟然这么小?那可不可以直接把10以内的质数列出来放一个数组里,然后在分母里面找都出现过哪几个质数乘一下就是最终的分母了……
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

使用道具 举报

本版积分规则

关闭

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

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

custom counter

GMT+8, 2017-5-28 01:40

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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