Quick Overview

Find the strongest and weakest five-card completions of a partial numeric hand under custom category and right-to-left tie-break rules while sharing one extensible comparison engine.

Find the Best and Worst Completion of a Partial Numeric Hand

Company: Rippling

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Complete a partial numeric card hand to five cards, returning the strongest and weakest possible completed hands under the game rules below. Cards are digits `1` through `9`, repetitions are allowed, and every appended position may use any digit independently. ## Function Contract Implement `complete_hands(partial_hand)` and return `[best_hand, worst_hand]`. Each result is a five-character string that begins with `partial_hand`. ## 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 multiplicities only; there are no suits, straights, or flushes. - Within the same category, compare the original five card positions from right to left; at the first difference, the larger digit is stronger. - If `partial_hand` already has length 5, return that hand as both the best and worst completion. - Use an extensible object-oriented category-evaluator abstraction so adding a category does not require rewriting `complete_hands` or the shared comparison flow. ## Constraints - `0 <= len(partial_hand) <= 5`. - Every character in `partial_hand` is a digit from `1` through `9`. - There are at most `9^5` possible full hands, but avoid duplicating category and tie-break logic between the best and worst searches. ## Examples ```text partial_hand = "99" output = ["99999", "99321"] ``` `"99999"` is five of a kind. Because the fixed prefix already contains a pair, one pair is the weakest attainable category; `"99321"` is the weakest such completion under the right-to-left tie-break.

Overview: Find the strongest and weakest five-card completions of a partial numeric hand under custom category and right-to-left tie-break rules while sharing one extensible comparison engine.

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

Complete a partial hand of zero to five digits to exactly five cards. Every missing position may independently be any digit from 1 through 9, and each returned hand must begin with the given prefix. Return a two-element list containing the strongest possible completion followed by the weakest. Categories from strongest to weakest are five of a kind, four of a kind, full house, two pair, three of a kind, one pair, and high card; classification uses multiplicities only. Within one category, compare original positions from right to left, with the first larger digit being stronger. If the input already has five cards, return it twice. Use an extensible object-oriented category-evaluator abstraction and share the same category and tie-break logic between both extrema.

Constraints

  • 0 <= len(partial_hand) <= 5.
  • Every character in partial_hand is a digit from 1 through 9.
  • Each appended position may independently use any digit from 1 through 9.
  • There are at most 9^5 possible complete hands.
  • Category evaluation uses an extensible object-oriented abstraction shared by the best and worst searches.

Examples

Input: ('99',)

Expected Output: ['99999', '99321']

Explanation: This is the source example: the fixed pair can become five of a kind, while one pair is the weakest attainable category.

Input: ('',)

Expected Output: ['99999', '54321']

Explanation: With no fixed cards, five nines is strongest and the right-to-left-minimal all-distinct hand is weakest.

Hints

  1. Give every complete hand one comparison key that captures both its category and the right-to-left tie-break.
  2. During one enumeration of all suffixes, maintain both the maximum and minimum key.

Loading coding console...

Show the approach

Approach

Use category evaluators that pair each strength rank with its sorted multiplicity pattern. For any complete hand, count its digits, find the matching evaluator, and build one comparison key from the rank followed by the original digits in right-to-left order. Enumerate every suffix of the required length with a depth-first search. At each completed hand, compute the shared key once and update both the greatest key and the least key. Every legal completion appears exactly once, so the greatest retained key is the strongest possible hand and the least retained key is the weakest. A length-five prefix reaches the same base case once and therefore becomes both results.

Time complexity:
O(9^(5 - n)), where n is len(partial_hand)
Space complexity:
O(5 - n) auxiliary recursion space