An interview question that I recently botched had to do with sorting a deck of cards programmatically. Assuming we start with an array of 52 card objects in order and we want to end up with an array of 52 card objects in a random order, how should we do it? The best way is to make the sorted array a List<Card> in C# and then one by one randomly remove items from the list and add them to the slots of the end array. You could have a number in a loop incrementing down from 51 and that could both denote the slot in the shuffled array to fill and a range for a random number with which to grab a position from the list.
No comments:
Post a Comment