《数据科学面试40+真题讲解》,K神本年度最后一次开课


一亩三分地论坛

 找回密码
 Sign Up 注册获取更多干货
码农求职神器Triplebyte:
不用海投,内推你去多家公司面试
Airbnb 数据科学职位
in analytics and inference
天天打游戏、照样领工资,
你要不要来?
把贵司招聘信息放这里
查看: 3147|回复: 8
收起左侧

Twitter Online Assessment (HackerRank Challenge)

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

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

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

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

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.. 1point 3acres 璁哄潧
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. Waral 鍗氬鏈夋洿澶氭枃绔,
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

. From 1point 3acres bbs

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.-google 1point3acres
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.. 1point 3acres 璁哄潧
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

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..鏈枃鍘熷垱鑷1point3acres璁哄潧

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

有一点比较奇怪的就是,听说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, 我想.

写个了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);
                R.first = R.first/gcd;
                R.second = R.second/gcd;. visit 1point3acres.com for more.
                int first = res.first * R.second + R.first  * res.second;
                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):. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
    return functools.reduce(add,lst)

#greatest common divider
def gcd(a,b):
    while a :
        a,b = b%a,a
    return b. more info on 1point3acres.com
.鐣欏璁哄潧-涓浜-涓夊垎鍦
#lowest common multiplier . Waral 鍗氬鏈夋洿澶氭枃绔,
def lcm(a,b):. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
    return a*b//gcd(a,b).鏈枃鍘熷垱鑷1point3acres璁哄潧
      
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 | 显示全部楼层
请问第一题该怎么做呢?
回复 支持 反对

使用道具 举报

本版积分规则

关闭

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

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2017-11-25 13:25

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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