KatInterview

Extensible Card Hand Evaluator with Pluggable Rules

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented designextensibilitygame logic

Last updated

Design and incrementally build a card-hand evaluator that ranks hands, resolves ties deterministically, adds new categories, and applies a swappable discard rule.

Extensible Card Hand Evaluator with Pluggable Rules

You are building a small card-game engine. The program receives a set of players, each holding a hand of cards, evaluates every hand, determines a winner, and returns a structured log of the entire game. The exercise is incremental: each part introduces a new rule, and the design should absorb those additions without rewriting what already works.

Card representation: A card has an integer rank (2–10, with 11 = Jack, 12 = Queen, 13 = King) and a suit. How ace is represented is intentionally left open — clarify it before coding.

Working assumption: Each hand contains three cards — the smallest count that can produce three of a kind. Verify this and the other open points with your interviewer.


Clarifying Questions to Ask First

  • How many cards per hand, and how many players per game?
  • Is an ace represented as 1, 14, or something else, and does it rank low or high?
  • When two hands fall in the same category, what breaks the tie (e.g., rank of the triple, then kickers)? Can a game end in a draw?
  • What does the returned log need to contain — one structured record per event, or a flat list of strings?
  • Are hands provided as input, or should the engine shuffle and deal from a deck?

Part 1 — Three of a Kind, Pair, High Card

Implement evaluation and comparison across three categories, ordered strongest to weakest: three of a kind > pair > high card.

Run a single game: evaluate every hand, identify the winner, and return the game log as a value (do not print it).

What to show:

  • A Card and Hand model that tracks rank, suit, and evaluated category explicitly
  • Deterministic tie-breaking within each category
  • The log accumulated during the game and returned, not side-effected
  • A structure that treats the category list and comparison logic as extension points

Part 2 — Insert a Flush Category

Add a flush: a hand whose three cards all share the same suit. The updated order is: three of a kind > flush > pair > high card.

What to show:

  • How inserting a new category between existing ones avoids touching the comparison logic
  • How two flush hands are ranked against each other
  • What happens when a hand qualifies for more than one category (e.g., three of a kind in the same suit) — define and state the priority rule

Part 3 — Add a Swappable Discard Rule

Before hands are compared, apply a discard rule that eliminates certain hands from the game. The specific rule for this part: any hand that is not exactly a pair is discarded, and that player drops out before comparison begins.

The rule is expected to change in future iterations, so the design should make swapping it straightforward.

Clarifying questions for this part:

  • Does three of a kind count as "contains a pair" for the purpose of this rule, or is it strictly excluded?
  • A flush ranks above a pair but contains no pair — is a flush hand discarded under this rule?
  • What is the result when every hand is discarded, or exactly one remains?
  • Should each discard appear in the log, and how much detail is expected?

What to show:

  • Where the discard rule lives in the design and how it could be replaced with a different rule
  • Correct handling of all edge cases the rule introduces
  • Log entries that make each discard and the final outcome fully traceable

Hints

Hint 1

Start by separating concerns into at least four things: a Card/Hand model, a category evaluator, a comparison strategy, and a game runner that owns the log. If you mix these, every new rule forces edits in multiple places.

Hint 2

Represent hand categories as ordered values (an enum with an explicit numeric rank, or a list that defines precedence). Then inserting a flush between existing categories is a one-line change, not a rewrite of comparisons.

Hint 3

Treat the discard rule as a strategy or a simple callable — something the game runner applies before comparison, not something baked into hand evaluation. That way swapping it in Part 4 costs one substitution.