Quick Overview

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.

Compare Two Five-Card Numeric Hands

Company: Rippling

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## 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 `1` through `9`. ## Examples ```text hand1 = "33322" hand2 = "99987" output = 1 ``` A full house outranks three of a kind under the specified order.

Overview: 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.

Read the full Rippling Software Engineer interview experience this question came from

Given two five-character hands whose cards are digits from 1 through 9, compare them under this game's multiplicity-based ranking. From strongest to weakest, the categories are five of a kind, four of a kind, full house, two pair, three of a kind, one pair, and high card. Suits, straights, and flushes do not exist. If both hands have the same category, compare their original card positions from right to left; the first larger digit wins, so the hands must not be sorted for this tie-break. Return 1 when hand 1 wins, 2 when hand 2 wins, and 0 for a tie. The category logic must use an extensible object-oriented evaluator abstraction so a new category can be added without rewriting the shared comparison flow.

Constraints

  • Both hands have length exactly 5.
  • Every character in each hand is a digit from 1 through 9.
  • Hands are classified only by digit multiplicities; there are no suits, straights, or flushes.
  • Equal categories are compared in original-position order from right to left.
  • Category evaluation uses an extensible object-oriented abstraction.

Examples

Input: ('33322', '99987')

Expected Output: 1

Explanation: This is the source example; a full house outranks three of a kind.

Input: ('12345', '12345')

Expected Output: 0

Explanation: Identical hands tie in both category and positional comparison.

Hints

  1. Express each category as a multiplicity pattern with a numeric strength rank.
  2. A single lexicographic key can combine category strength with the required right-to-left tie-break.

Loading coding console...

Show the approach

Approach

Represent each category evaluator by its strength rank and its sorted multiplicity pattern. Counting a hand's digits yields exactly one of those patterns, so the first matching evaluator supplies the category rank without placing category-specific branches in the comparison routine. Form a key from that rank followed by the hand's digits in right-to-left order. Lexicographic comparison of two keys first compares categories and, only when they match, applies the required positional tie-break. Thus a larger key identifies exactly the stronger hand; equal keys identify a tie. Because every hand contains exactly five cards, all counting and comparison work is bounded by a constant.

Time complexity:
O(1)
Space complexity:
O(1)