Expected Draws Until a Second Distinct Token Shows Up
Math & Bit ManipulationAuto-graded · 16 test casesLast updated
Compute, as a floating-point number, the expected number of draws from a fixed three-outcome distribution until two different outcomes have appeared, using conditioning and geometric waiting times.
Draws Until Two Different Tokens
A machine hands out one token per draw. Each token is one of three kinds: Red, Blue, or Green. Every draw is independent and uses the same fixed probabilities:
- Red: 1/2
- Blue: 1/4
- Green: 1/4
You keep drawing until your collection contains at least two different kinds of token, and then you stop. For instance, if the draws come out as Red, Red, Blue, you stop after the third draw, so that run used 3 draws.
Let X be the number of draws made in a run. Write a function that returns the expected value E[X] for the probabilities above.
The function takes no arguments. Return the answer as a Python float. The result is compared to the expected value with floating-point tolerance, so any mathematically equivalent computation is fine.
Constraints
- Exactly three kinds of token, with probabilities 1/2, 1/4, and 1/4.
- Draws are independent and identically distributed.
- The process ends the first time two distinct kinds have been seen.
- Return a single
float.
Examples
Example 1
Input: expected_draws_two_kinds()
Output: 2.666666666666666
Hints
Hint 1
The first draw never ends the process, because only one kind has been seen. Split the analysis by what the first token was.
Hint 2
If the first token has probability p, each later draw is a repeat with probability p and something new with probability 1 - p. How many extra draws does that take on average?
Hint 3
Combine with the law of total expectation: E[X] = 1 + sum over kinds of p * 1/(1 - p). Evaluate it for 1/2, 1/4, 1/4.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Condition on the kind of the first token.
-
The first draw always happens and always yields some kind
i, with probabilityp_i. -
After that, each draw is either a repeat of kind
i(probabilityp_i) or a different kind (probability1 - p_i). The process stops at the first "different" draw, so the number of additional draws is geometric with success probability1 - p_i. Its mean is1 / (1 - p_i). -
Given first kind
i, the expected total is1 + 1 / (1 - p_i). -
Average over the first kind with the law of total expectation:
E[X] = sum_i p_i * (1 + 1 / (1 - p_i)) = 1 + sum_i p_i / (1 - p_i)
Plugging in the numbers:
| First kind | p | Expected total draws | Weighted |
|---|---|---|---|
| Red | 1/2 | 1 + 2 = 3 | 3/2 |
| Blue | 1/4 | 1 + 4/3 = 7/3 | 7/12 |
| Green | 1/4 | 1 + 4/3 = 7/3 | 7/12 |
Sum: 3/2 + 7/12 + 7/12 = 8/3, about 2.6667.
Why it is correct
The draws are independent, so after the first token the future depends only on which kind was drawn first. The waiting time for "anything other than kind i" is exactly geometric, and the law of total expectation combines the three cases without overlap. The result exceeds 2, as it must, since at least two draws are always needed.
Complexity
Time O(k) and space O(1) for k kinds (here k = 3), which is constant.
Reference solution
def expected_draws_two_kinds() -> float:
probs = [0.5, 0.25, 0.25]
# One guaranteed first draw, then a geometric wait for a different kind.
total = 0.0
for p in probs:
total += p * (1 + 1 / (1 - p))
return total