Next Ace Probability Given at Least One Ace Already Drawn
Arrays & HashingAuto-graded · 21 test casesLast updated
Tests conditional probability over sampling without replacement, combining hypergeometric counting with Bayes' theorem to handle a "at least one" conditioning event.
Setup
You have a deck of n cards, exactly a of which are "Aces" (special cards). Cards are drawn one at a time, uniformly at random, without replacement.
You are told that among the first m cards drawn, at least one is an Ace. Under that condition, what is the probability that card number m + 1 is also an Ace?
Return the answer as a Python float.
What you are computing
Let K be the count of Aces among the first m cards. You want:
P(card m+1 is an Ace | K ≥ 1)
Because draws are without replacement, the number of Aces remaining in the deck after the first m draws depends on K. You must average over all possible values of K ≥ 1, weighted by their correct probabilities.
Key facts
-
Marginal uniformity. Because every ordering of the deck is equally likely, each individual card position is marginally uniform over all
ncards. The unconditional probability that any single position holds an Ace isa / n. -
Conditional depletion. Given the exact contents of the first
mdraws, the next card is uniform over then − mremaining cards. Ifkof those firstmwere Aces, the probability the next card is an Ace equals(a − k) / (n − m). -
No independence shortcut. Drawing without replacement makes card positions dependent. You cannot treat card
m+1as independent of cards1throughm.
Exact formula hint
Let q = P(K = 0) be the probability that none of the first m cards are Aces. This follows a hypergeometric distribution:
q = C(n − a, m) / C(n, m) if m ≤ n − a
q = 0 otherwise
You may find it useful to express the answer in terms of a, n, m, and q using Bayes' theorem and the tower property of conditional expectation.
Constraints
1 ≤ a ≤ n1 ≤ m ≤ n − 1a + m ≤ nis not guaranteed; it is possible for the conditioning event (at least one Ace in the firstm) to be certain (q = 0).- The conditioning event always has positive probability: you may assume the inputs are such that
P(K ≥ 1) > 0(i.e.,a ≥ 1and it is possible to draw an Ace inmdraws). - The answer must be returned as a
float. ncan be as small as 2 and as large as several thousand; use exact arithmetic internally to avoid floating-point drift, then convert at the end.
Examples
Example 1
Input: next_ace_probability(52, 4, 2)
Output: 0.059393939393939395
Example 2
Input: next_ace_probability(10, 3, 4)
Output: 0.26
Example 3
Input: next_ace_probability(5, 2, 4)
Output: 0.4
Hints
Hint 1
Start by computing q = P(no Ace in the first m draws) using the hypergeometric formula C(n−a, m) / C(n, m). This single quantity lets you express the conditioning event probability as 1 − q.
Hint 2
Apply Bayes' theorem: P(next is Ace | K ≥ 1) = P(next is Ace AND K ≥ 1) / P(K ≥ 1). For the numerator, think about what P(K ≥ 1 | next card is an Ace) looks like — if you fix one specific card as an Ace, the first m draws come from the remaining n−1 cards with a−1 Aces.
Hint 3
Use the Fraction class from Python's fractions module for all intermediate calculations to keep arithmetic exact. Only convert to float at the final return step.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core idea: Bayes' theorem in reverse
The most direct route is to apply Bayes' theorem, expressing the conditional probability we want in terms of quantities we can compute exactly from the hypergeometric distribution.
Let E be the event "at least one Ace in the first m draws" (i.e., K ≥ 1), and let A* be the event "card m+1 is an Ace". We want P(A* | E).
By Bayes:
P(A* | E) = P(A*) · P(E | A*) / P(E)
Each factor has a clean form:
P(A*) — marginal uniformity of the deck gives a / n. Every card position is equally likely to hold any card, so unconditionally the probability of an Ace at position m+1 is exactly a / n.
P(E) — the complement of no Ace in the first m draws. The count of Aces in any fixed set of m positions follows a hypergeometric distribution:
P(K = 0) = q = C(n − a, m) / C(n, m) if m ≤ n − a, else 0
P(E) = 1 − q
P(E | A*) — the probability of at least one Ace in the first m draws, given that position m+1 is an Ace. Conditioning on that specific card being an Ace means we fix one Ace at position m+1, and the first m cards are drawn from the remaining n − 1 cards, which contain a − 1 Aces and n − a non-Aces. So:
P(K = 0 | A*) = C(n − a, m) / C(n − 1, m) if m ≤ n − a, else 0
P(E | A*) = 1 − C(n − a, m) / C(n − 1, m)
Substituting into Bayes gives the answer.
Why this is correct
The argument rests on two facts that are both provable from the uniform distribution over permutations:
- Any single card position is marginally uniform — it holds an Ace with probability
a / n. - Conditioning on a specific position holding an Ace, the remaining positions are a uniform draw from the other
n − 1cards.
No independence assumption is needed. The hypergeometric counts handle the dependence introduced by sampling without replacement.
Edge case checks
- A = 1. The single Ace must already be in the first
mdraws (conditioning event holds), so there are zero Aces left. The formula gives 0. Concretely,P(E | A*) = 1 − C(n−1, m) / C(n−1, m) = 0. - q = 0 (m > n − a, so an Ace in the first m is certain). Both
P(E)andP(E | A*)become 1, and the formula collapses to the unconditionala / n. This is correct: the conditioning event has probability 1 and carries no information.
Implementation note
Use Python's Fraction class for all intermediate arithmetic. The binomial coefficients can be large integers, and Fraction keeps the calculation exact. Convert to float only at the final step.
Complexity
Time: O(m) to compute comb(n, m) and comb(n−1, m) (Python's math.comb runs in O(m)). Space: O(1) beyond the arbitrary-precision integers.
from math import comb
from fractions import Fraction
def next_ace_probability(n: int, a: int, m: int) -> float:
non_aces = n - a
# q = P(K = 0): probability none of the first m cards are Aces
# Uses hypergeometric: C(non_aces, m) / C(n, m)
no_ace_first_m = (
Fraction(comb(non_aces, m), comb(n, m))
if m <= non_aces else Fraction(0, 1)
)
# P(K = 0 | card m+1 is an Ace):
# Fix card m+1 as one specific Ace. The first m draws come from the
# remaining n-1 cards, which contain (n-1) - (a-1) = non_aces non-Aces.
# So this is C(non_aces, m) / C(n-1, m).
no_ace_first_m_given_next_ace = (
Fraction(comb(non_aces, m), comb(n - 1, m))
if m <= non_aces else Fraction(0, 1)
)
# P(K >= 1) = 1 - q
event = 1 - no_ace_first_m
# P(K >= 1 | card m+1 is Ace) = 1 - P(K=0 | card m+1 is Ace)
event_given_next_ace = 1 - no_ace_first_m_given_next_ace
# Bayes: P(next is Ace | K>=1) = P(next is Ace) * P(K>=1 | next is Ace) / P(K>=1)
# P(next is Ace) = a/n by marginal uniformity
return float(Fraction(a, n) * event_given_next_ace / event)