I have a single direction linked list without knowing its size.
I want to get a random element in this list, and I just have one time chance to traverse the list. (I am not allowed to traverse twice or more)
What’s the algorithm for this problem? Thanks!
Disadvantages of Linked Lists:Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do a binary search with linked lists.
Count the number of nodes by traversing the list. Traverse the list again and select every node with a probability of 1/N. The selection can be done by generating a random number from 0 to N-i for the node, and selecting the i'th node only if the generated number is equal to 0 (or any other fixed number from 0 to N-i).
The time complexity in this case is O(n).
This is just reservoir sampling with a reservoir of size 1.
Essentially it is really simple
This is uniformly sampled, since the probability of picking any element at the end of the day is 1/n (exercise to the reader).
This is probably an interview question.Reservoir sampling is used by data scientist to store relevant data in limited storage from large stream of data.
If you have to collect k elements from any array with elements n, such that you probability of each element collected should be same (k/n), you follow two steps,
1) Store first k elements in the storage. 2) When the next element(k+1) comes from the stream obviously you have no space in your collection anymore.Generate a random number from o to n, if the generated random number is less than k suppose l, replace storage[l] with the (k+1) element from stream.
Now, coming back to your question, here storage size is 1.So you will pick the first node,iterate over the list for second element.Now generate the random number ,if its 1, leave the sample alone otherwise switch the storage element from list
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