FastPrepRolling Until One Face's Own Total Passes a Threshold
Problems/Rolling Until One Face's Own Total Passes a Threshold

Rolling Until One Face's Own Total Passes a Threshold

Optiver logoOptiver● HardFULLTIMEOA

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A single 2026 report about Optiver's online test for full-time quant researchers lists practice questions on probability; this problem comes from one of those questions.

That question rolls an ordinary fair die with six sides, gives each face its own running total, ends once the total belonging to a single face goes over 120, and asks for the mean of everything rolled. No answer is given.

What we decided: the number of sides and the threshold are inputs (at most 20 sides and threshold 1000; the report has 6 and 120); going over is read as strictly greater, so a face total equal to the threshold does not end the game (if reaching 120 were enough, the reported case would give about 394.855); the roll that ends the game counts toward the total; results are decimals graded to 10^-9 in relative terms. Example 1 is the reported case; all limits and the remaining examples are our own picks.

Our version is about 70% the same as the reported question.

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) → double

Examples

Example 1

faces = 6threshold = 120return = 414.6123428918134

Face 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.25

Face 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.0

Any 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 <= 20
  • 0 <= threshold <= 1000
  • A returned value x passes 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

Optiver interview experiences

  • Optiver SWE Intern OA: CPU Temperature Simulation: 1 Experience
  • Optiver Trading Intern Market-Making Games: 1 Experience
  • Optiver Quant Trader Intern Technical Interview: 1 Experience
  • Optiver SWE Intern System Design Interview: 15 Experiences
All Optiver interview questionsSee Optiver hiring insights
Saved
class Solution {
    public double expectedRollSum(int faces, int threshold) {
        // Write your code here.
    }
}
Checking account…