Can Your Unfinished Poker Hand Overtake a Showdown Hand?
Arrays & HashingAuto-graded · 22 test casesLast 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)
- High Card: none of the patterns below.
- One Pair: two cards of equal rank.
- Two Pair: two different pairs.
- Three of a Kind: three cards of equal rank.
- 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 the5. No wrap-around such asQ K A 2 3. - Flush: five cards of one suit.
- Full House: three of a kind plus a pair.
- Four of a Kind: four cards of equal rank.
- 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_handhas exactly 5 cards, anddraw_handhas 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task splits into two parts.
- Rank a five-card hand. Write an evaluator that maps a hand to a tuple
(category, tiebreakers...). Python compares tuples element by element, so tuple comparison reproduces the poker ordering, and equal tuples mean a tie. - Try every completion. Remove all cards of both hands from the 52-card deck. Enumerate every way to pick the
5 - len(draw_hand)missing cards from what is left. If any completed hand has a tuple strictly greater than the revealed hand's tuple, returnTrue; if none does, returnFalse.
The evaluator
Categories are numbered 0 (high card) to 8 (straight flush), and the tuple continues with the ranks that matter, most important first:
| Category | Tuple |
|---|---|
| Straight flush | (8, top) |
| Four of a kind | (7, quad, kicker) |
| Full house | (6, triple, pair) |
| Flush | (5, r1..r5) descending |
| Straight | (4, top) |
| Three of a kind | (3, triple, k1, k2) |
| Two pair | (2, high pair, low pair, kicker) |
| One pair | (1, pair, k1, k2, k3) |
| High card | (0, r1..r5) |
Key details:
- Build
(count, rank)groups sorted in descending order, so the most frequent rank comes first and equal counts are ordered by rank. - A straight needs five distinct ranks with
max - min == 4, or the special set{2,3,4,5,14}(the wheel), whose top card is 5. - Test categories from strongest to weakest where they could overlap (straight flush before the others, full house before flush, flush before straight).
- Suits are only used to detect flushes, never to break ties.
Why it is correct
The tuple order matches the poker rules: category first, then the defining ranks, then the kickers. Equal tuples occur exactly when the hands tie. The search covers every legal completion using only unseen cards, so if a winning completion exists it is found, and if none exists the loop ends with False. The strict > ensures ties are not counted as wins.
Complexity
- Time: at most C(44, 2) = 946 completions (with 3 known cards), or 43 (with 4), each evaluated in constant time. Effectively O(1).
- Space: O(1), at most a 44-card deck;
combinationsis lazy.
Reference solution
from collections import Counter
from itertools import combinations
from typing import List
RANKS = '23456789TJQKA'
SUITS = 'CDHS'
RANK_VALUE = {r: i + 2 for i, r in enumerate(RANKS)}
def evaluate_hand(hand):
ranks = sorted((RANK_VALUE[c[0]] for c in hand), reverse=True)
counts = Counter(ranks)
groups = sorted(((count, rank) for rank, count in counts.items()), reverse=True)
flush = len({c[1] for c in hand}) == 1
unique = sorted(set(ranks))
straight_high = 0
if len(unique) == 5:
if unique[-1] - unique[0] == 4:
straight_high = unique[-1]
elif unique == [2, 3, 4, 5, 14]:
straight_high = 5
if straight_high and flush:
return (8, straight_high)
if groups[0][0] == 4:
quad = groups[0][1]
kicker = next(r for r in ranks if r != quad)
return (7, quad, kicker)
if groups[0][0] == 3 and groups[1][0] == 2:
return (6, groups[0][1], groups[1][1])
if flush:
return (5, *ranks)
if straight_high:
return (4, straight_high)
if groups[0][0] == 3:
trip = groups[0][1]
kickers = sorted((r for r in ranks if r != trip), reverse=True)
return (3, trip, *kickers)
pairs = sorted((rank for rank, count in counts.items() if count == 2), reverse=True)
if len(pairs) == 2:
kicker = next(r for r in ranks if r not in pairs)
return (2, *pairs, kicker)
if len(pairs) == 1:
pair = pairs[0]
kickers = sorted((r for r in ranks if r != pair), reverse=True)
return (1, pair, *kickers)
return (0, *ranks)
def can_overtake(revealed_hand: List[str], draw_hand: List[str]) -> bool:
target = evaluate_hand(revealed_hand)
used = set(revealed_hand) | set(draw_hand)
deck = [r + s for r in RANKS for s in SUITS if r + s not in used]
needed = 5 - len(draw_hand)
for extra in combinations(deck, needed):
if evaluate_hand(list(draw_hand) + list(extra)) > target:
return True
return False