1 Comment Already

commenter
scheffler Said,
May 12th, 2008 @9:08 pm  

I’m no algorithm guru by any stretch of the imagination, but I have enjoyed working on these questions you’ve been throwing out there.

I came up with a solution which I *think* is reasonable and was performant over huge sets. Seems deceptively simple though. So, I was curious what other solutions there were to this problem.

My approach, in a nutshell, was to iterate over the list, once. For each element in the array I requested a new random number and used that number as an index into the array. I then swapped the contents of that index value with the current slot. Lather, rinse, repeat.

The problem statement didn’t specify whether I could shuffle the contents of the passed in array or whether I had to make a copy. I opted for shuffling in place to keep things easier.

Since my approach comment kinda spoils the interview question format I’m not sure you’ll want to post this. But I would be interested in knowing how others approach this.

Love your blog, keep em coming!

Related Post

Please Leave Your Comments Below

Please Note: All comments are moderated, so it may take some time before your comment appears.