KatInterview

Can Your Unfinished Poker Hand Overtake a Showdown Hand?

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashingsimulationbrute-forcecombinations

Last updated

Decide whether a 3 or 4 card poker hand can be filled from the unseen deck into a five-card hand that strictly outranks a revealed five-card hand.

Poker Draw Check

Two players hold cards from one standard 52-card deck. Player A's five-card hand is fully revealed. Player B holds only some of their cards so far (3 or 4) and still needs to receive the rest from the cards nobody has seen yet.

Decide whether Player B can possibly finish with a five-card hand that strictly beats Player A's hand.

Card format

Each card is a two-character string: a rank followed by a suit.

  • Ranks, lowest to highest: 2 3 4 5 6 7 8 9 T J Q K A
  • Suits: C, D, H, S

Hand categories (weakest to strongest)

  1. High Card: none of the patterns below.
  2. One Pair: two cards of equal rank.
  3. Two Pair: two different pairs.
  4. Three of a Kind: three cards of equal rank.
  5. Straight: five cards with consecutive ranks. The ace may be high (T J Q K A) or low (A 2 3 4 5). The low version is the weakest straight, and its top card counts as the 5. No wrap-around such as Q K A 2 3.
  6. Flush: five cards of one suit.
  7. Full House: three of a kind plus a pair.
  8. Four of a Kind: four cards of equal rank.
  9. Straight Flush: a straight where all cards share one suit.

Comparing two five-card hands

  • A higher category wins.
  • With equal categories, compare the ranks that define the hand first (for example, the rank of the pair, or of the three of a kind), and then the remaining cards from highest to lowest. For a straight or straight flush, only the top card of the straight matters. For a full house, compare the triple's rank, then the pair's rank. For two pair, compare the higher pair, then the lower pair, then the leftover card.
  • Suits never break ties. Hands that match in all of the above are equal, and an equal hand is not a win.

Task

Implement the function. The unseen cards are all 52 cards minus every card in both hands. Return True if some choice of unseen cards, filling Player B's hand up to exactly five cards, produces a hand strictly stronger than Player A's. Otherwise return False.

  • revealed_hand: Player A's five cards.
  • draw_hand: Player B's current 3 or 4 cards.

Constraints

  • revealed_hand has exactly 5 cards, and draw_hand has 3 or 4 cards.
  • All cards across both lists are valid and distinct.
  • Missing cards may only come from the unseen part of the deck (cards in neither list).

Examples

Example 1

Input: can_overtake(['9C', '9D', '4S', '7H', '2D'], ['AS', 'KS', 'QS', 'JS'])

Output: True

Example 2

Input: can_overtake(['AS', 'AC', 'AD', 'AH', 'KS'], ['2C', '3D', '4H', '5S'])

Output: False

Hints

Hint 1

Turn every five-card hand into a tuple like (category, tiebreak ranks...) so that ordinary tuple comparison gives the correct poker ordering, including ties.

Hint 2

The number of ways to finish the hand is tiny (at most C(44, 2) = 946), so try every completion drawn from the deck after removing all cards in both hands.

Hint 3

Be careful with the A-2-3-4-5 straight (top card is 5, not ace), with checking categories from strongest to weakest, and with using strict greater-than so ties do not count.