3 ms·
>shuffle a deck of cards Fwiw this is surprisingly subtle and non-trivial to get right. A naive "random swap" approach does not produce uniform permutations. S
by krackers 2y ago
>shuffle a deck of cards
Fwiw this is surprisingly subtle and non-trivial to get right. A naive "random swap" approach does not produce uniform permutations. Sorting based on a random comparison function can likewise produce biased results depending on the implementation of the sort. Tagging each element with a random number and then sorting based on the tag works (or so I think?) but you need to deal with the case where tags collide. Fisher-yates shuffle is bulletproof but I would not trust myself to write it from memory lest you end up writing Sattolo's algorithm instead.
- gumby 2y agoIt’s an open ended problem and someone could nerd out on the maths while another could define a card class, a suit class, and use a factory pattern (oh brother…SMH but probably wouldn’t categorically rule someone out for it, just talk some more). At the end of the day the question is: “could this person do the job and could they work with others in the process?”
- chuckadams 2y agoThe problem isn’t about getting the perfect answer, it’s about observing the process, and conversing about the answer afterwards. I’d be fine hiring the candidate who whiffs the shuffle but knows about Fisher-Yeats and says they’d look for a well-tested implementation and drop it in. I mean that can’t be their answer to everything, but evidence of real experience should grant a Mulligan now and then. Coding interviews cannot be code themselves.