KatInterview

Next Red Card: Conditional Draw Probability

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashingprobabilitycombinatoricsconditional-probability

Last 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 far
  • b — 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 is 26/52 = 0.5.
  • If all 26 red cards have already appeared (r = 26), the probability is 0.0.
  • If all 26 black cards have already appeared (b = 26), every remaining card is red, so the probability is 1.0.

Constraints

  • 0 <= r <= 26
  • 0 <= b <= 26
  • r + 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.