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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With