How would you pick a uniform random element in linked list with unknown length in one pass or if not two passes?
Use reservoir sampling http://en.wikipedia.org/wiki/Reservoir_sampling . You need only one pass of the data.
For picking one element:
I will let you prove that this results in uniform selection of elements.
1.4m articles
1.4m replys
5 comments
57.0k users