Technology07 Oct 2007 09:41 pm
Interview questions: Random selection from a linked list
There is a linked list of numbers of length N. N is very large and you don’t know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random.
This one seems pretty simple. Walk the list, generating a new random number for each entry. Keep a ‘top k’ chart of the highest random numbers, and their associated entries. When we hit the end of the list, the numbers in the chart are the required random numbers.
Anyone have better methods?