KatInterview

Eventual Winner Odds in a Draw-Two Card Duel with Replays

Math & Bit ManipulationAuto-graded · 16 test cases
EasyAuto-gradedMath & Bit Manipulationprobabilitysymmetrycounting

Last 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:

  1. Player A draws one card uniformly at random from the full deck.
  2. Player B draws one card uniformly at random from the remaining cards.
  3. The player whose card has the higher rank wins.
  4. 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) <= 1000
  • 0 <= rank_counts[i] <= 1000
  • At least two entries of rank_counts are 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).