KatInterview

Expected Draws Until a Second Distinct Token Shows Up

Math & Bit ManipulationAuto-graded · 16 test cases
EasyAuto-gradedMath & Bit Manipulationprobabilityexpected-valuegeometric-distribution

Last 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.