So a few days ago, I got an amusing idea for an interview question which I realized was totally pointless as an interview question, because it has no practical value whatsoever. So instead, I'm going to post it on my blog, as a way to help waste the time of all my CS friends. There is no prize whatsoever for a correct answer, except for the
(
Read more... )
Comments 13
That said, if you meant the former, the first part of the answer is that (2) is more efficient, as it will never repeat. If you meant the latter, (2) is the only one guaranteed to give a solution, as a single, re-used random permutation may have a cycle through a subset of the solution space, whereas brute force covers the entire solution space.
Clarifying now the question: When you say "by how much", there's no real answer, as (1) has no guarantee of a solution through any finite number of iterations (again, assuming its possible permutations cover the entire solution space.) That forces us to pick an expected chance of success in order to come up with a meaningful comparison of the two.
Reply
For the latter: Mean runtime over a large, uniformly selected sample of input arguments. (i.e., input arguments are equally likely to be in any permutation of sorted order)
Reply
However, I should point out that if you were interviewing Teela Brown, her randomly selected sample would already be sorted.
Reply
Oh, and I have no idea what the answer is, that not being my field at all.
Reply
Reply
Reply
Iterating over all permutations is sampling without replacement. That's n choose 1, (in other words, there are n! permutations), and on average, we'll find the correct one halfway through. So it's O(n!), with a constant of .5.
Random iterations is sampling with replacement, and the sample space is again all the permutations So it becomes a geometric distribution with probability of success 1/n!, and the expected time to success is 1/(1/n!), or n!. (but without the reduced constant.)
So random permutations is twice as inefficient.
Going to peek now!
Reply
Reply
Reply
Reply
*sigh* Yony, you can tell I miss you, right? :-) (btw, there's a pic of the 3 of us up on my website)
I usually ask them "What question did you want me to ask that I didn't?" or "Anything you want to tell me that I didn't ask about?". I'll be adding this one immediately! ;-)
Reply
Leave a comment