KatInterview

Forced Winner in Complete and Incomplete Card Hands

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingsimulationcombinatoricscounting

Last updated

Rank five-card hands with a tuple key, then decide whether an outcome is forced across all ways to fill in missing cards.

Two players are dealt cards in a simplified poker-style game. Each hand is given as a list of card strings. A card is written as rank followed by suit, for example "10H" or "AS".

  • Rank is one of 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A. Ranks increase in that order, and the ace is always the highest.
  • Suit is one of S, H, D, C. Suits never affect strength.

Write a function that takes the first player's cards and the second player's cards and reports who wins.

Ranking a complete hand

A complete hand has exactly 5 cards. Categories, from strongest to weakest:

  1. Four of a kind
  2. Full house
  3. Three of a kind
  4. Two pair
  5. One pair
  6. High card

There are no straights or flushes in this game. A hand such as 2, 3, 4, 5, 6 is just a high-card hand.

If both hands are in the same category, compare these ranks in order. The first difference decides the winner:

CategoryComparison order
Four of a kindrank of the four matching cards, then the remaining card
Full houserank of the three matching cards, then rank of the pair
Three of a kindrank of the three matching cards, then the other two cards from highest to lowest
Two pairhigher pair, then lower pair, then the remaining card
One pairrank of the pair, then the other three cards from highest to lowest
High cardall five ranks from highest to lowest

If every comparison is equal, the hands are tied.

Incomplete hands

Sometimes a hand has fewer than 5 cards because some have not arrived yet. In that case, consider every valid way to fill in the missing cards:

  • Each final hand has exactly 5 cards.
  • The missing cards come from the standard 52-card deck, excluding the cards already present in either hand.
  • No card may appear twice among the final 10 cards, including across the two hands.

Return:

  • "user1" if the first player wins in every valid completion,
  • "user2" if the second player wins in every valid completion,
  • "tie" if every valid completion is a tie,
  • "unknown" in all other cases.

When both hands already have 5 cards, there is exactly one completion, so the result is simply "user1", "user2", or "tie".

Constraints

  • Each hand has between 0 and 5 cards.
  • All cards given across both hands are distinct and valid.
  • Rank 10 is written with two characters, for example "10D".
  • The return value is exactly one of "user1", "user2", "tie", "unknown".
  • Suits are ignored when comparing strength.

Examples

Example 1

Input: judge_card_duel(['AS', 'AH', 'AD', 'AC', '2S'], ['KS', 'KH', 'KD', 'KC', '3S'])

Output: 'user1'

Example 2

Input: judge_card_duel(['2S', '2H', '2D', '3S', '3H'], ['AS', 'AH', 'AD', 'KS', 'KH'])

Output: 'user2'

Example 3

Input: judge_card_duel(['AS', 'KH', 'QD', 'JC', '9S'], ['AD', 'KC', 'QH', 'JS', '9D'])

Output: 'tie'

Example 4

Input: judge_card_duel(['AS', 'AH', 'AD'], ['KS', 'KH'])

Output: 'unknown'

Hints

Hint 1

Turn each complete hand into a tuple such as (category, tiebreak1, tiebreak2, ...) so that ordinary tuple comparison matches the rules.

Hint 2

Count how often each rank appears and sort the (count, rank) pairs in descending order. The shape of the largest groups gives the category. Remember to strip only the last character of a card so that 10 is parsed correctly.

Hint 3

For incomplete hands, enumerate completions: choose the first player's missing cards from the unused deck, then the second player's from what is left. Stop with unknown as soon as two different outcomes have appeared.