Rolling Until One Face's Own Total Passes a Threshold
Updated
Problem statement
You hold a fair die whose faces sides show the numbers 1 to faces. Every roll gives each of these numbers chance 1/faces, independent of the rolls before it. You roll it again and again.
Each number keeps a total of its own, starting at 0. When a roll shows f, add f to the total of number f and leave the other totals alone. For example, with six sides, after the rolls 4, 2, 4 the total of 4 is 8, the total of 2 is 2, and the other four totals are still 0.
The game ends on the roll that first lifts one of these totals strictly above threshold. A total equal to threshold does not stop you.
What you return is the expected total of every number you rolled, the roll that made you stop included. A relative error no larger than 10^-9 is accepted.
Function
expectedRollSum(faces: int, threshold: int) → doubleExamples
Example 1
faces = 6threshold = 120return = 414.6123428918134Face 1 needs 121 showings to pass 120, face 2 needs 61, then 41, 31, 25 and 21 for faces 3 to 6. On average the game takes about 118.4607 rolls, and each roll averages 3.5, so the expected total is about 414.6123.
Example 2
faces = 2threshold = 3return = 5.25Face 1 passes 3 on its fourth showing and face 2 on its second. Zero rolls and one roll always leave the game running. After two rolls it ends only if both were 2s, so it is still running with chance 3/4; after three rolls, with chance 4/8 (at most one 2); after four rolls, with chance 4/16 (exactly one 2 and three 1s); five rolls always end it. Expected rolls 1 + 1 + 3/4 + 1/2 + 1/4 = 3.5, times the average roll 1.5, gives 5.25.
Example 3
faces = 3threshold = 0return = 2.0Any single roll puts its face total above 0, so the game always ends after one roll. The result is the average roll, (1 + 2 + 3) / 3 = 2.
Constraints
1 <= faces <= 200 <= threshold <= 1000- A returned value
xpasses when|x - correct| <= 10^-9 · correct. Every correct value is at least 1.
More Optiver Online Assessment questions
- Chance a Die Game Balance Reaches ZeroOA · Seen Dec 2026
- 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
- 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
- Magic Number Range CounterGraviton · OA
- At Most K Jumps with Quadratic Transition CostInfosys · OAInfosys interview questions
- Maximum Signal Walk in a Server TreeTower Research Capital · OA
- Count Nondecreasing Digit-Sum ArraysMicrosoft · OAMicrosoft interview questions
- Jump Game with Prime-3 StepsUber · OAUber interview questions
- Match an XOR-Adjusted SumSquarepoint Capital · OASquarepoint Capital interview questions
- Count Numbers with a Prime Digit SumRippling · OARippling interview questions
- Count Permutations Without Three Equal Parities in a RowGoogle · OAGoogle interview questions
- TikTok UserTikTok · OATikTok interview questions
- String Transformation (Also for Infra Automation Intern)Snowflake · OASnowflake interview questions