FastPrepOdd First-Success Roll Probability

Odd First-Success Roll Probability

IMC Trading logoIMC Trading● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

For this exercise, assume a fair die has faces numbered from 1 through sides, with every face equally likely. Repeated rolls are independent. Roll until the first occurrence of face sides.

Let N be the total number of rolls, starting at 1 and including the successful roll. Return the probability that N is odd.

The reported question uses a six-sided die and asks about the first 6. For this exercise, assume sides is an input generalizing that die size. Complete oddFirstSuccessProbability(sides) and return a double without rounding.

Answers are accepted when abs(actual-expected) <= 1e-9 * max(1,abs(expected)).

Function

oddFirstSuccessProbability(sides: int) → double

Examples

Example 1

sides = 6return = 0.5454545454545454

Reported fixed case under stated assumptions. The exact probability is 6/11.

Example 2

sides = 2return = 0.6666666666666666

Authored practice example. The exact probability is 2/3.

Example 3

sides = 3return = 0.6

Authored practice example. The exact probability is 3/5.

Constraints

  • For this exercise, assume 2 <= sides <= 10^6.
  • The die is fair; rolls are independent.
  • The total count includes the successful roll.

More IMC Trading problems

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