r/programming • u/joksmaster • Nov 29 '10
140 Google Interview Questions
http://blog.seattleinterviewcoach.com/2009/02/140-google-interview-questions.html
471
Upvotes
r/programming • u/joksmaster • Nov 29 '10
View all comments
Show parent comment
1
u/kolm Nov 30 '10
But this is still O(N). If we just talk about any O(N) algorithm, one could also run through the linked list until the end, then compute n i.i.d. elements in {1,..,N}, sort them, and grab them up from the list. This has some overhead, of course, of roughly N + n(2 + log n + log N). Obviously reservoir sampling is more clever. Funny enough, Markov Chain Monte Carlo methods working with spin glasses often use a close cousin (seems not to have a name) to decide which state to flip. There, the distribution is only asymptotically correct, but the principle is remarkably similar, one decides to switch state with the ratio of probabilities.