由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 贴个刚才的电话面试题
相关主题
问道 面试题MS面试题
分享一道google 面试题。大数据相关。问一个不知道算是软工还是C++的面试题
问码工一个问题Google的电话面试题
昨天的google面试题请问一个简单的面试题
[合集] 昨天的google面试题Bloomberg 面试题请教
解一道 GOOGLE 面试题 ...coding question
贡献亚马逊面试题发一道面试题
Adobe面试题, 怎么能把很多文件读到Memory ?一道MS面试题
相关话题的讨论汇总
话题: preline话题: line话题: file话题: 概率话题: 遍历
进入JobHunting版参与讨论
1 (共1页)
c*e
发帖数: 15
1
因为没见过这个题,所以想了好半天才答上来
面试感觉一般,答得不顺利,move on了
一个很长的text文档,怎样遍历一边,随机输出其中的一行
限制:
1.只能遍历一边
2.随机均匀分配
================
我的答案是:
保存已经遍历的行中的一个preLine
每读入第n行,在line_n和preLine中选一个,update preLine.
line_n的概率是1/n, preLine的概率是(n-1)/n
读到文件尾return preLine.
l*x
发帖数: 14021
2
I can think of two possible solutions, in both solutions we calculated the
random number first.
1. Since it is a text file, can we get the size of the file? As we read
through the file, we can calculate the size of all the lines have been read.
Once the size read / size all = the random number, the current line is the
line to return.
2. If the random number is X out of 100, save the lines from (x/100) *
current total to the current line. e.g is X is 50 out of 100, current total
line number is 20, save lines 10 to 20. Once we reach the end of the file,
the first line being saved is the line to be returned. This way, we can only
need to save up to half of the whole file.
If the X is less than 50/100, assume we can read the file backward.
c*******t
发帖数: 1095
3
LZ right answer
LS complicated and may not be allowed
l*x
发帖数: 14021
4
汗,读贴不仔细。以为LZ保存读过的每条line。
k*******a
发帖数: 772
5
这个不是老题目了吗
用一个cache
读第一行,保存
第二行,1/2概率 replace
第三行,1/3概率 replace
。。。
第n行, 1/n概率 replace
1 (共1页)
进入JobHunting版参与讨论
相关主题
一道MS面试题[合集] 昨天的google面试题
请教一个新鲜算法面试题解一道 GOOGLE 面试题 ...
[合集] 问个google面试题贡献亚马逊面试题
问2道面试题Adobe面试题, 怎么能把很多文件读到Memory ?
问道 面试题MS面试题
分享一道google 面试题。大数据相关。问一个不知道算是软工还是C++的面试题
问码工一个问题Google的电话面试题
昨天的google面试题请问一个简单的面试题
相关话题的讨论汇总
话题: preline话题: line话题: file话题: 概率话题: 遍历