Ctrl K

Chance of Not Going Over

Medium
In a board game a player starts with 0 points. While the score is below stop_at, the player pulls a token from a bag and adds its value to the score. Each token's value is a random integer from 1 to max_step, every value equally likely, and every pull is independent of the others. As soon as the score reaches stop_at or more, the player stops. Return the probability that the final score is at most cap. Answers within 10⁻⁵ of the true value are accepted.
Example 1
cap=4, stop_at=1, max_step=8 → 0.5

The player draws exactly one token: 4 of the 8 equally likely values (1 to 4) keep the score at or below 4, so the chance is 4/8 = 0.5.

Example 2
cap=3, stop_at=2, max_step=2 → 1.0

A first token of 2 (chance 1/2) ends the game at 2. A first token of 1 is followed by a 1 or a 2, ending at 2 or 3. The score never goes above 3, so the chance is 1.0.

Example 3
cap=2, stop_at=2, max_step=2 → 0.75

The only bad outcome is a 1 followed by a 2, which ends at 3. Its chance is 1/2 · 1/2 = 0.25, so the answer is 0.75.

Constraints
  • 0 ≤ stop_at ≤ cap ≤ 10⁴
  • 1 ≤ max_step ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWhich Pot Runs Dry First MediumLast Viewer in the Right Seat MediumOdds of Exactly That Many Heads MediumA Fair Split of Colored Beads HardCounting Grid Routes Medium
loading Python…
Input
cap=4, stop_at=1, max_step=8
Expected
0.5

Run checks the first 3 tests in your browser. Submit checks all 66 tests on the server, hidden ones included.