Next Red Card: Conditional Draw Probability
Arrays & HashingAuto-graded · 19 test casesLast updated
Tests conditional probability under sampling without replacement from a fixed-composition deck given observed draw history.
Problem
A standard deck holds 52 cards: exactly 26 red and 26 black. The deck is shuffled uniformly at random, meaning every ordering is equally likely. Cards are turned over one at a time from the top, without replacement.
At some point during the draw, you pause and record your observations:
r— the number of red cards seen so farb— the number of black cards seen so far
Your task is to compute the probability that the very next card you turn over is red, given only this information.
What You Need to Return
Return a float representing the probability that the next card drawn is red.
How to Think About It
Because every permutation of the full deck was equally likely to begin with, the cards still sitting in the deck are in a uniformly random order. That means the next card is a uniform draw from whatever remains.
At the moment you pause:
- Total cards drawn:
r + b - Red cards remaining:
26 − r - Total cards remaining:
52 − r − b
The requested probability follows directly from these counts.
Edge Cases
- If no cards have been drawn yet (
r = 0, b = 0), the answer is26/52 = 0.5. - If all 26 red cards have already appeared (
r = 26), the probability is0.0. - If all 26 black cards have already appeared (
b = 26), every remaining card is red, so the probability is1.0.
Constraints
0 <= r <= 260 <= b <= 26r + b < 52(at least one card remains in the deck)- The deck always starts with exactly 26 red and 26 black cards
Examples
Example 1
Input: next_card_red_probability(2, 0)
Output: 0.48
Example 2
Input: next_card_red_probability(0, 0)
Output: 0.5
Example 3
Input: next_card_red_probability(13, 13)
Output: 0.5
Hints
Hint 1
After removing r red and b black cards from the deck, ask yourself: how many red cards are left, and how many cards total are left?
Hint 2
Because the deck was shuffled uniformly, the remaining cards are in a uniformly random order — so the next position is equally likely to hold any one of the remaining cards.
Hint 3
The answer is a single division: (reds remaining) / (cards remaining). Both quantities are determined directly by r and b.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Insight: Uniform Order Over Remaining Cards
The deck was shuffled so that every permutation is equally likely. Once you remove the r + b cards you have already seen, the cards that remain are still arranged in a uniformly random order — the shuffle gives no special structure to any particular slot. This means the next card is drawn uniformly at random from whatever is left in the deck.
Because the draw is uniform over the remaining cards, the probability of drawing red is simply the fraction of red cards among those remaining.
Counting What Remains
| Quantity | Value |
|---|---|
| Red cards remaining | 26 − r |
| Black cards remaining | 26 − b |
| Total cards remaining | 52 − r − b |
Formula
$$ P(\text{next card is red} \mid r, b) = \frac{26 - r}{52 - r - b} $$
Why This Is Correct
This is straightforward conditional probability under sampling without replacement. Given the exact composition of the remaining deck and the fact that the remaining cards are in a uniformly random order, every card in the remaining deck is equally likely to be in the next position. The probability of the event "next card is red" equals the count of red cards divided by the count of total cards remaining.
Note that this result also follows from a symmetry (exchangeability) argument: within the remaining deck, all positions are equivalent. The next position has the same probability of being red as any other remaining position, which equals the proportion of reds remaining.
Edge Cases
r = 0, b = 0: returns26/52 = 0.5. No information, half the deck is red.r = 26: returns0/26 = 0.0. All reds are gone.b = 26: returns26/26 = 1.0. Every remaining card is red.
Complexity
- Time: O(1) — two subtractions and one division.
- Space: O(1) — no additional memory used.
def next_card_red_probability(r: int, b: int) -> float:
return (26 - r) / (52 - r - b)