KatInterview

Design a Five-Card Hand Evaluator with Partial Hand Completion

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented designclassificationcombinatorics

Last updated

Model poker-style five-card hands with an abstract type hierarchy, compare two hands by strength and tie-break, then find the strongest and weakest completions of a partial hand.

Five-Card Hand Evaluator

A card game is played with hands of exactly five cards. Every hand belongs to exactly one of seven categories, ranked from strongest to weakest:

RankNameDescription
1Five of a kindAll five cards share the same label
2Four of a kindExactly four cards share a label
3Full houseThree cards share one label, the remaining two share another
4Three of a kindThree cards share a label; the other two are distinct from it and each other
5Two pairTwo cards share one label, two others share a second label, the fifth differs
6One pairExactly two cards share a label; the other three are all different
7High cardAll five labels are distinct

There is no upper limit on duplicates per label, so five of a kind is achievable. A stronger category always beats a weaker one regardless of labels.

Constraints

  • A complete hand contains exactly five cards.
  • Card labels are single characters drawn from a defined, ordered alphabet (clarify the exact set and ordering before coding).
  • When two hands share the same category, resolve the tie by comparing cards left to right in dealt order, using the label ranking.
  • A hand's category must be determined once at construction time, not recomputed on every comparison.

Part 1 — Object model and comparison

Design an abstract class HandType and a class Hand. Before writing any code, explain:

  • What HandType abstracts and how each of the seven categories extends it.
  • What data Hand stores and how it determines its type at construction.
  • How evaluate(hand_a, hand_b) decides which hand is stronger.

Then implement the classes and the function. State the time complexity of constructing a hand and of a single comparison. Address input validation: what happens when a hand has the wrong number of cards or contains an unknown label?


Part 2 — Best and worst completions of a partial hand

You are given between zero and four known cards; the rest are unknown. Return the strongest possible complete hand (best_hand) and the weakest possible complete hand (worst_hand) that both include all of the known cards.

Example: if the two known cards are both 9, the strongest completion is 99999 and the weakest is 99321 (assuming 1 is the lowest-ranked label).

A brute-force approach is acceptable. Implement it using the classes from Part 1, and provide a step-by-step time-complexity breakdown. Then discuss whether a direct construction could replace enumeration, and what that approach would need to guarantee.

Clarify before coding:

  • Does the position of each known card in the final hand matter, or is a hand an unordered multiset?
  • What should the function return if zero cards are known? If more than five are provided?
  • When multiple completions tie for best or worst, which one is returned?

Hints

Hint 1

For HandType, the only thing that varies across the seven categories is the matching rule and the rank weight — everything else is shared behavior. A frequency map (label → count) over the five cards gives you all the information you need to classify any hand in O(1) for a fixed hand size.

Hint 2

Comparison happens in two stages: first compare the integer weights of the two HandType objects; only if those are equal do you fall through to a left-to-right card-by-card comparison using label rank. Make sure the tie-break order (dealt order vs. group size) is pinned down before you write the comparator.

Hint 3

For Part 2, the number of possible labels is a small constant L (at most 13 in a standard deck). You are choosing at most 5 cards from L labels with repetition allowed, so the search space is at most L^5, which is bounded and polynomial. Enumerate all valid completions, score each with your Part 1 hand, and track the running best and worst using your comparator.