传说中的谷歌招聘委员会成员之一,从幕后走出来,教你学系统设计!


一亩三分地论坛

 找回密码
 获取更多干活,快来注册
Babeltime游戏工作室招工程师、美术和策划
Tubi TV招安卓、前端和机器学习工程师
把贵司招聘信息放这里
查看: 2988|回复: 8
收起左侧

Twitter Online Assessment (HackerRank Challenge)

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

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

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

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

x
1. Two Operations (Programming).鏈枃鍘熷垱鑷1point3acres璁哄潧
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.. Waral 鍗氬鏈夋洿澶氭枃绔,
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..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.
Output
You have to print two space separated integers, num den, where num and den are numerator and denominator of the sum respectively.. Waral 鍗氬鏈夋洿澶氭枃绔,
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-google 1point3acres
2 4.鐣欏璁哄潧-涓浜-涓夊垎鍦
2 3. From 1point 3acres bbs
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的邮件就来了_(:з」∠)_. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

(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);. 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){
                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;. Waral 鍗氬鏈夋洿澶氭枃绔,
                gcd = _gcd(first, second);
                res.first = first/gcd; 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
                res.second = second/gcd;.鏈枃鍘熷垱鑷1point3acres璁哄潧
        }        . 1point3acres.com/bbs
        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. 鍥磋鎴戜滑@1point 3 acres

#lowest common multiplier
def lcm(a,b):
    return a*b//gcd(a,b)
      
def add(a,b):
   
    de = lcm(a[1],b[1]). more info on 1point3acres.com
    nu = a[0]*de//a[1] + b[0]*de//b[1]. Waral 鍗氬鏈夋洿澶氭枃绔,
    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-9-21 15:07

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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