Probability the Third Card Is an Ace After an Ace Appears
Arrays & HashingAuto-graded · 23 test casesLast updated
Compute a conditional probability for drawing three cards without replacement by splitting on how many aces the first two draws contain.
A deck holds total_cards distinct cards, of which exactly ace_count are aces. Three cards are drawn uniformly at random, one after another, without replacement (so every ordered triple of distinct cards is equally likely).
Given that at least one of the first two drawn cards is an ace, return the probability that the third drawn card is an ace.
Implement third_ace_given_ace_in_first_two(total_cards, ace_count) and return the conditional probability as a float.
Details:
- The conditioning event is "the first card is an ace OR the second card is an ace" (possibly both). Everything is computed relative to that event, not to the unconditioned deck.
- Returns
0.0when the third card cannot possibly be an ace under the condition (for example, a deck with a single ace). - Answers are compared with a small floating-point tolerance, so any correct evaluation order is fine.
Constraints
3 <= total_cards <= 10**61 <= ace_count <= total_cards- The conditioning event always has positive probability, so the conditional probability is well defined.
Examples
Example 1
Input: third_ace_given_ace_in_first_two(52, 4)
Output: 0.0593939393939394
Example 2
Input: third_ace_given_ace_in_first_two(3, 1)
Output: 0
Example 3
Input: third_ace_given_ace_in_first_two(4, 2)
Output: 0.39999999999999997
Hints
Hint 1
Split the conditioning event into two mutually exclusive cases: exactly one ace among the first two cards, and exactly two aces among the first two cards.
Hint 2
If the first two cards contain exactly one ace, an ace is left among the `total_cards - 2` unseen cards; if they contain two aces, one fewer ace remains.
Hint 3
Weight each case by its probability *given* that at least one ace is already in the first two cards, and count the two-case probabilities from the number of unordered pairs.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Look only at the unordered pair formed by the first two cards. There are C(total_cards, 2) such pairs, all equally likely. The conditioning event — at least one ace among the first two cards — covers every pair except those made of two non-aces, so its probability is
P(cond) = (C(total_cards, 2) - C(total_cards - ace_count, 2)) / C(total_cards, 2)
Split the conditioning event into two disjoint cases:
- Exactly one ace in the pair:
ace_count * (total_cards - ace_count)such pairs. With one ace already gone,ace_count - 1aces remain among thetotal_cards - 2unseen cards, so the third card is an ace with probability(ace_count - 1) / (total_cards - 2). - Exactly two aces in the pair:
C(ace_count, 2)such pairs. Nowace_count - 2aces remain, giving(ace_count - 2) / (total_cards - 2).
Multiply each case count by its conditional third-card probability and divide by C(total_cards, 2) to get the joint probability of "condition holds AND third card is an ace". Finally divide by P(cond) to condition on the event.
Why it is correct
The two cases partition the conditioning event, and the third card is drawn uniformly from the total_cards - 2 cards not yet seen, so the per-case probabilities are exact. Summing the two disjoint contributions gives P(cond ∩ third ace), and dividing by P(cond) is the definition of conditional probability. The arithmetic is O(1) and uses exact binomial counts, so no approximation beyond normal float rounding enters.
Complexity
Time O(1), space O(1).
from math import comb
def third_ace_given_ace_in_first_two(total_cards: int, ace_count: int) -> float:
pair_count = comb(total_cards, 2)
conditioning_count = pair_count - comb(total_cards - ace_count, 2)
exactly_one_ace_count = ace_count * (total_cards - ace_count)
exactly_two_aces_count = comb(ace_count, 2)
joint_probability = (
exactly_one_ace_count * (ace_count - 1) / (pair_count * (total_cards - 2))
+ exactly_two_aces_count * (ace_count - 2) / (pair_count * (total_cards - 2))
)
return joint_probability / (conditioning_count / pair_count)