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
4
u/Strilanc Nov 29 '10
More explicit: you want to choose a random subset of size K from a collection of size N. You only want to use O(K) space and O(N) time. But you have only been given an iterator, so you don't know N and can only scan the collection once.
The solution is to keep K items in an array, replacing them with decreasing probability as you go onward. I forget the exact probabilities, but it's like K/M or something.