Amazon Interview Question
Software Engineer / DevelopersBut we have to choose one sentence finally.. so shd we make N i.e available space as 1 ? as we cannot choose 1 from the last remaining N because then it is not making it random across all sentence? Please correct me if, I am wrong.
I am not sure and unable to understand how your solution would be as good as returning any random (w.r.t the entire stream of sentences) as this solution expects ?
I think Reservoir sampling will work for this problem if we set N as 1. Suppose there are total m sentences, the possibility for i-th sentence being chosen is :
1/i x i/(i+1) x (i+1)/(i+2) x ... x (m-2)/(m-1) x (m-1)/m = 1/m
1st iteration-
temp = read 1st sentence;
i=2;
while((sentence = read i-th sentence from stream) != null) { //keep reading till stream is exhausted
if random() > i/(i+1){
temp = sentence;
}
}
return temp;
what is random() returning here....decimal or whole number?
If whole number how would this logic work?
for instance if random() is returning 7 and if we have 10 lines in stream we expect the logic to return 7th line from the stream.
This logic would return 10th line, becos for every iteration always random() > i / (i+1)is true. ( 7 > 2/3 , 7 > 3/4, 7 > 4/5 and so on...)
it's classic reservoir sampling problem, say space limit is N,
- Raynor March 21, 2010first fill sentences into the space until full, then start from N+1 element in stream, get random number between 1 and N+1, and if the number is smaller than N,
then put this one into space, and we also have to randomly remove one that is already in space by get a random number between 1 and N, then keep doing this till stream exhausted.