Hey Everyone,
I recently had an screening interview at Google, which I passed but in the feedback they said I did not provide the optimal solution. I was wondering what is the optimal solution to that problem:
Imagine you are given a stream of non-negative integers, until you receive a zero and in that case you need to print a number from the stream with the distribution of the numbers in the stream.
i.e: [1,2,3,1,1,3,0] will have the probabilities array [0.5, 1/6, 2/6] and we have to pick a random number from that.
The solutions I proposed were:
Is there any third solution which is more optimized than the second one ? I thought about creating the probabilities on the fly but I wasn't able to make it work.
The question itself does feel a little bit like this one - https://leetcode.com/problems/random-pick-with-weight/description/ the only difference is the stream
Thanks for you help :)