Shuffle, deal and sort a four-player card hand
I was asked this
No reports yet. Sign in to say where you were asked it.
You are building the core of a four-player card game. Model a standard 52-card deck and implement the deal.
Types to define
-
Suit:CLUBS,DIAMONDS,HEARTS,SPADES, in this order. -
Rank:A,2,3,4,5,6,7,8,9,10,J,Q,K, in this order (ace low). -
Card: one suit and one rank. Each of the 52 combinations appears exactly once in a deck. -
Deck: holds the 52 cards and can shuffle them. -
Player: has a name and a hand of cards.
Implement Game.deal_and_sort_hands() (dealAndSortHands in Java or C++). It must:
- Shuffle the deck uniformly at random. Every ordering must be equally likely.
- Deal every card so each of the four players receives the same number of cards (13 each).
- Sort each player's hand by suit (in the
Suitorder above), then by rank (in theRankorder above). - Return each player's sorted hand (for example, a map from player name to list of cards).
Requirements
- Ranks must sort by their order in the game, not as strings:
9 < 10 < J < Q < K. - Calling the method again must produce a fresh, complete deal. Cards must not carry over or pile up from the previous one.
- Make the randomness injectable (for example, a seed) so tests are deterministic.
- If the number of players does not divide the deck evenly, raise an error and leave the state unchanged.
Example
With players North, East, South, West and some seed, one possible result is:
North: 7C 2D 5D 6D 9D QD 2H 7H KH 2S 3S 4S JS
East: 6C QC 3D KD 4H 9H 10H AS 5S 6S 9S QS KS
...
Each hand has 13 cards and all 52 cards appear exactly once across the four hands. Clubs come before diamonds, then hearts, then spades, and inside a suit 10 sorts after 9.
Example (edge case)
With a single player, the hand is the whole deck in order: AC 2C ... KC AD ... KS. With three players, the call fails, because 52 is not divisible by 3.
Be ready for follow-ups: playing tricks where players must follow the led suit, ace-high or trump ordering, and other player counts.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.

