Chance a Die Game Balance Reaches Zero
Updated
Problem statement
A game starts with a balance of start. A fair die with faces faces, labeled 1 to faces, is rolled rolls times. On each roll every face is equally likely, and the rolls are independent of each other.
After each roll, the face shown changes the balance: even faces raise it by the face value, odd faces lower it by the face value. The balance is looked at after every roll, including the final one. Return the chance that at least once it is 0 or less. A balance of exactly 0 counts. Once that has happened, the rolls that follow do not change the outcome.
Function
ruinProbability(start: int, rolls: int, faces: int) → doubleExamples
Example 1
start = 12rolls = 8faces = 6return = 0.13720755220240816As a fraction this is 28807/209952: 230,456 of the 1,679,616 equally likely sequences of eight rolls bring the balance to 0 or below after one of the rolls.
Example 2
start = 3rolls = 2faces = 4return = 0.3125Faces 2 and 4 add, faces 1 and 3 subtract. A first roll of 3 leaves exactly 0, which counts as ruin: chance 1/4. After a first roll of 1 the balance is 2, and a 3 on the second roll makes it -1: chance 1/4 of 1/4 = 1/16. A first roll of 2 or 4 leaves 5 or 7, more than the last roll can take away. The total is 1/4 + 1/16 = 5/16.
Example 3
start = 15rolls = 3faces = 6return = 0.004629629629629629One roll takes away at most 5, so only three 5s in a row reach 0, and they leave exactly 0. That has chance 1/216. Starting from 16, the answer would be 0.
Constraints
1 <= start <= 10^91 <= rolls <= 1002 <= faces <= 20- The checker accepts an answer when
|output - exact| <= 10^-9.
More Optiver Online Assessment questions
- Chance a Hand of Cards Multiplies to More Than a ThresholdOA · Seen Dec 2026
- Chance a Sum of Random Draws Has a Given Number of DigitsOA · Seen Dec 2026
- Expected Draws to Remove All Green CountersOA · Seen Dec 2026
- Expected Profit of a Doubling Betting SystemOA · Seen Dec 2026
- Expected Steps Until a Random Walk Gets Far From ZeroOA · Seen Dec 2026
- Rolling Until One Face's Own Total Passes a ThresholdOA · Seen Dec 2026
- Two Ranked Teams Meeting in a Random BracketOA · Seen Dec 2026
- Overheat Prevention ControllerOA · Seen Nov 2026
- Power Cell BankOA · Seen Nov 2026
- Count Straight-Line Word Occurrences in a GridOA · Seen Sep 2026
Similar questions at other companies
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Count Lucky TicketsThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Jump Game with Prime-Step RuleUber · OAUber interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Unique Paths Through a GridPeopleStrong · OAPeopleStrong interview questions
- Get Minimum OperationsJP Morgan Chase · OAJP Morgan Chase interview questions
- Jump Game with Prime-3 StepsAgoda · OAAgoda interview questions
- Find Encrypted NumberAmazon · OA
- Prime StringSnowflake · OASnowflake interview questions