KatInterview

Dealing and Ranking Three-Card Hands With an Extensible Rule Set

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designsimulationcomparison

Last updated

Build a small card game that deals three cards per player from one deck, ranks hands by category, picks the winner, then extend the ranking with a suit-based category.

Write a program that simulates one round of a simple three-card game. A single shuffled 52-card deck (13 ranks, 4 suits) is shared by all players, and each player is dealt three cards. The program must print every player's hand and announce the winner.

The exercise has two stages. The first ignores suits. The second adds a suit-dependent category, so structure the first stage so that the change is small.

Rules that apply throughout

  • Cards are dealt without replacement, so no card repeats within a round.
  • Rank order from low to high is 2 through 10, J, Q, K, A. The ace is always high.
  • No straights exist. Only the categories listed for the current stage are recognized.

Stage 1: suits are irrelevant

Categories from strongest to weakest:

  1. Trips: all three cards share a rank.
  2. Pair: exactly two cards share a rank.
  3. High card: anything else.

Within a category, hands are compared like this:

  • Trips: by the shared rank.
  • Pair: first by the rank of the pair, then by the remaining single card.
  • High card: by the highest card, then the middle card, then the lowest card.

Stage 2: add a flush

A flush is a hand where all three cards share a suit. The order becomes trips > flush > pair > high card. Two flushes are compared by highest card, then middle, then lowest. Update the program accordingly.

What to decide and state up front

  • Number of players (fixed or a parameter; at most 17 fit in one deck).
  • What happens when hands are exactly equal in strength: shared win, or an extra tie-break.
  • How results are reported (printed, returned, or both).
  • Whether the deal can be seeded so tests are reproducible.

What is expected

  • Clean representations for cards, the deck and hands.
  • Correct detection of each category and correct comparison inside each category.
  • A well-defined outcome for exact ties.
  • A design where the category order lives in one place, so Stage 2 is a small change.
  • Tests showing, for example, that a flush beats a pair in Stage 2 but not in Stage 1.

Hints

Hint 1

Turn each hand into a single comparable key, such as a tuple of (category strength, tie-break ranks...). Then picking the winner is just taking the maximum key.

Hint 2

For tie-break ranks, order the cards by how many times their rank appears, then by rank, both descending. This handles trips, pairs (pair rank before the kicker) and high cards with one rule.

Hint 3

Keep the category order in one table or enum and make each category a small detection rule. Adding the flush then means adding one rule and one entry. Also inject the random source so tests can fix the deal.