KatInterview

Next Ace Probability Given at Least One Ace Already Drawn

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingconditional-probabilitycombinatoricshypergeometric

Last 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 n cards. The unconditional probability that any single position holds an Ace is a / n.

  • Conditional depletion. Given the exact contents of the first m draws, the next card is uniform over the n − m remaining cards. If k of those first m were 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+1 as independent of cards 1 through m.


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 ≤ n
  • 1 ≤ m ≤ n − 1
  • a + m ≤ n is not guaranteed; it is possible for the conditioning event (at least one Ace in the first m) 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 ≥ 1 and it is possible to draw an Ace in m draws).
  • The answer must be returned as a float.
  • n can 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.