Compare Two Five-Card Numeric Hands
RipplingSoftware Engineer · Technical ScreenLast updated
Compare two five-digit numeric hands under a nonstandard multiplicity ranking and original-position right-to-left tie-break, using extensible category evaluators rather than standard poker rules.
Problem
Compare two five-card hands in a numeric card game. Cards are digits 1 through 9, repetitions are allowed, and the category ranking is intentionally different from standard poker.
Function Contract
Implement compare_hands(hand1, hand2). Each hand is a five-character digit string. Return 1 if hand 1 wins, 2 if hand 2 wins, or 0 for a tie.
Rules
- Categories from strongest to weakest are: five of a kind, four of a kind, full house, two pair, three of a kind, one pair, high card.
- Classify a hand by its multiplicities only; there are no suits, straights, or flushes.
- When categories match, compare original cards from right to left. At the first differing position, the larger digit wins.
- Do not sort a hand for the tie-break.
- Use an extensible object-oriented category-evaluator abstraction so adding a category does not require rewriting the comparison flow.
Constraints
- Both hands have length exactly 5.
- Every character is a digit from
1through9.
Examples
hand1 = "33322"
hand2 = "99987"
output = 1
A full house outranks three of a kind under the specified order.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.