FastPrepAll Even Faces Before the First Odd Roll

All Even Faces Before the First Odd Roll

IMC Trading logoIMC Trading● EasyINTERNPHONE SCREEN
Learn

Problem statement

Roll a die repeatedly. Find the probability that every even-numbered face appears at least once before the first odd-numbered result.

For this exercise, assume the die is fair, has an even number sides of faces numbered consecutively from 1 through sides, and all rolls are independent. Repeated even results are allowed and do not reset the faces already observed. Stop at the first odd result; an odd result before every even face has appeared means failure.

For this exercise, assume sides is an input generalizing the conventional six-sided interpretation of the reported question. Complete allEvenBeforeOddProbability(sides) and return the event probability as a double without rounding.

For this exercise, assume answers are accepted when abs(actual - expected) <= 1e-9 * max(1, abs(expected)).

Function

allEvenBeforeOddProbability(sides: int) → double

Examples

Example 1

sides = 6return = 0.05

Under the stated conventional six-sided interpretation, ignore repetitions of previously seen even faces. The chances that the next new face is even are 3/6, 2/5 and 1/4. Their product is 1/20 = 0.05. This value is independently derived, rather than an author-confirmed answer.

Example 2

sides = 2return = 0.5

This authored example has only one even face. Success occurs exactly when the first roll is 2.

Example 3

sides = 4return = 0.16666666666666666

This authored example requires both 2 and 4 before 1 or 3. The probability is (2/4)*(1/3) = 1/6.

Constraints

  • For this exercise, assume 2 <= sides <= 20 and sides is even.
  • Each face has probability 1/sides on every independent roll.
  • Success requires all sides/2 distinct even labels before any odd result.

More IMC Trading problems

See IMC Trading hiring insights
public double allEvenBeforeOddProbability(int sides) {
    // Write your solution here.
}
sides6
expected0.05
Checking account…