KatInterview

Four-Card Hand Ranker with Extensible Type System

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designoopdesignsimulation

Last updated

Tests object-oriented design skills by building a modular, open/closed hand classifier and comparator for a custom four-card game.

Four-Card Hand Ranker

You are building the core logic for a two-player card game called Dune Deck. Each player holds exactly 4 cards. Your job is to classify each hand and decide which player wins.

Card Values

Cards are labeled 1 through 9. 9 is the strongest and 1 is the weakest.

Hand Types

Every hand falls into exactly one category. Listed from strongest to weakest:

RankNameDescriptionExample
1stFour of a KindAll four cards identical9999
2ndTwo PairTwo distinct pairs2332
3rdThree of a KindExactly three cards identical, one different9998
4thOne PairExactly one pair, two distinct others5233
5thHigh CardAll four cards different2345

This ordering is intentional and non-standard. Two Pair outranks Three of a Kind in Dune Deck. Implement the order exactly as listed above.

Determining the Winner

  1. By type: the hand with the stronger type wins outright.
  2. By position (tiebreak): if both hands share the same type, compare cards right to left in dealt order (do not sort). Compare the last card of each hand; if they differ, the higher value wins. If equal, move one position left and repeat. If every position matches, the result is a TIE.

Your Task

Implement a function that accepts two hands and returns which player wins:

  • Return "HAND_1" if the first hand is stronger.
  • Return "HAND_2" if the second hand is stronger.
  • Return "TIE" if both hands are completely equal under the rules above.

Each hand may arrive as a 4-character string (e.g. "2332") or as a list of four values (e.g. ['2','3','3','2'] or [2,3,3,2]). Your solution must accept either form.

Design Requirement

The game will introduce additional hand types in future updates. Structure your solution so that a new hand type can be added by writing new code only — the classification loop, the comparator, and the evaluator function must require zero edits.

Constraints

  • Each hand contains exactly 4 cards.
  • Card labels are single digits in the range 1–9 inclusive.
  • Input is either a plain string of four digit characters or a list of four single-character strings or integers.
  • You may assume all inputs are valid and well-formed.
  • Optimize for clarity and extensibility. With only 4 cards, raw performance is irrelevant.

Examples

Example 1

Input: evaluate('2332', '9998')

Output: 'HAND_1'

Example 2

Input: evaluate('9999', '8888')

Output: 'HAND_1'

Example 3

Input: evaluate('5233', '5332')

Output: 'HAND_1'

Example 4

Input: evaluate('1234', '4321')

Output: 'HAND_1'

Hints

Hint 1

Use a sorted list of frequency counts (e.g., [3, 1] for three-of-a-kind) as a fingerprint — each hand type maps to a unique fingerprint, and matching against it keeps classification fully self-contained per type class.

Hint 2

Give each hand-type class a numeric rank attribute and store all types in a single registry tuple. The classification loop just walks that tuple and returns the first match — adding a new type means appending to the tuple, nothing else.

Hint 3

For the right-to-left tiebreak, use reversed() on the normalized card tuples and zip them together. Never sort the cards for comparison; the dealt order is intentional and sorting would give wrong results for hands like '5233' vs '5332'.