Eventual Winner Odds in a Draw-Two Card Duel with Replays
Math & Bit ManipulationAuto-graded · 16 test casesLast updated
Compute the probability that the first of two drawers eventually wins a rank-comparison game with replayed ties, using counting and a symmetry argument.
Draw-Two Card Duel
A deck contains cards of several ranks. The ranks are listed from lowest to highest, and rank_counts[i] is the number of cards of the i-th lowest rank. A standard deck has 13 ranks with 4 cards each, but your function must handle any list of counts.
A round works as follows:
- Player A draws one card uniformly at random from the full deck.
- Player B draws one card uniformly at random from the remaining cards.
- The player whose card has the higher rank wins.
- If both cards have the same rank, the round is a tie. Both cards go back into the deck, the deck is shuffled, and a fresh round begins with the full deck.
Rounds repeat until someone wins. Return the probability that Player A is the one who eventually wins the game.
Cards of the same rank are indistinguishable for comparison purposes, and suits play no role.
Input
rank_counts: a list of non-negative integers, ordered from the lowest rank to the highest.
Output
- A
float: the probability that Player A eventually wins.
Constraints
2 <= len(rank_counts) <= 10000 <= rank_counts[i] <= 1000- At least two entries of
rank_countsare positive, so a round can end with a winner and the game ends with probability 1. - Ties are always replayed with the full deck restored, so every round is an identical, independent experiment.
Examples
Example 1
Input: eventual_first_player_win_chance([4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4])
Output: 0.5
Hints
Hint 1
Count the ordered pairs of distinct cards (first drawn, second drawn) where the first card has the higher rank. Compare that with the count where the second card is higher.
Hint 2
Every ordered pair of two different physical cards is equally likely to be drawn. Swapping the two cards in a pair turns an A-wins outcome into a B-wins outcome.
Hint 3
Tied rounds are discarded and the experiment restarts unchanged, so the eventual probability is P(A wins a round) divided by P(a round is not a tie).
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat each round as choosing an ordered pair of two different physical cards, with every such pair equally likely. Let W be the number of ordered pairs (x, y) where x has a strictly higher rank than y.
Swapping the two cards in a pair is a one-to-one correspondence between ordered pairs where the first card is higher and ordered pairs where the second card is higher. So the number of ordered pairs where the second card is higher is also W. This holds even though the draws are made without replacement, because every ordered pair of distinct cards has the same probability.
Per round:
- P(A wins) = W / T
- P(B wins) = W / T
- P(tie) = 1 - 2W / T
where T is the number of ordered pairs of distinct cards.
Tied rounds are discarded and the identical experiment restarts. The eventual probability that A wins is therefore the probability that A wins conditional on the round being decisive:
P(A eventually wins) = (W / T) / (2W / T) = 1/2.
Neither player has an advantage. The code computes W by sweeping the ranks in increasing order and adding count * cards_below for each rank, then forms the ratio. The constraint that at least two ranks are non-empty guarantees W > 0, so there is no division by zero.
Complexity
- Time: O(n), where n is the number of ranks.
- Space: O(1).
Reference solution
from typing import List
def eventual_first_player_win_chance(rank_counts: List[int]) -> float:
cards_below = 0
decisive_pairs = 0 # ordered (first, second) card pairs where first has higher rank
for count in rank_counts:
decisive_pairs += count * cards_below
cards_below += count
# By symmetry, the number of ordered pairs where the second card is higher
# is exactly the same, so A wins half of all decisive outcomes.
return decisive_pairs / (2 * decisive_pairs)