Monday, January 5, 2009

Picking an Element Randomly from Stream

Problem: Given a finite stream of data elements, develop an algorithm to pick an element from the stream, randomly. Algorithm should guarantee that, every element of the stream has equal probability of getting picked. Additionally, we don't know the size of the stream until the stream actually finishes. We also have constant space, so all the elements of the stream can not be stored.

Hint: Once we see an element from the stream, we either store it or discard it with some probability, p.

Solution: show

2 comments:

Anonymous said...

Related post: http://blog.smola.org/post/1077104724/random-elements-from-a-stream#disqus_thread

Apptitude test said...

You made numerous nice ideas there. I done a search on the issue and learnt nearly all peoples will agree with your blog.