Give you:
- A stream (end of the stream is EOF)
- A function
next()
to get the next element in the stream and advance the pointer in the stream - A random generator generating floats between 0 and 1 (inclusively) uniformly
Output:
- An element that is proven to be randomly (uniformly distributed) chosen
You can one or two variables.
You are not allowed to use array / list, and you cannot tell the way that trying to get all elements out and store them all and then pick
.
This is an interview question.
My thinking is:
- I use a var
cur
to store most recent kept element - So, if i get a new element, I generate a random 0 or 1 using generator, if it is 0 then
cur = new element
; otherwise, continue; - If I get EOF, then return cur
Is my thinking correct? How to prove?
Here is a same question
How would you pick a uniform random element in linked list with unknown length?