FastPrepExpected Steps Until a Random Walk Gets Far From Zero
Problems/Expected Steps Until a Random Walk Gets Far From Zero

Expected Steps Until a Random Walk Gets Far From Zero

Optiver logoOptiver● HardFULLTIMEOA

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem is based on one entry in a list of probability practice questions, taken from one 2026 report on Optiver's online assessment (the full-time quant researcher track).

In that entry a walk begins at zero and moves by a nonzero step of at most 3 in either direction, the six choices being equally likely; the question wants the average count of steps needed to get 12 or more away from zero. No solution is included.

How we set it up: the biggest step and the distance become inputs (limits 10 and 1000, against 3 and 12 in the report); a step that overshoots the distance also ends the walk, and the final step is counted; answers are decimals, and we allow a relative error of one part in a billion (10^-9). The first example uses the reported numbers; the limits and Examples 2 and 3 are ours.

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

A marker begins at 0 on a number line and moves in steps. Each step shifts it by an integer amount k where 1 <= |k| <= maxStep, so there are 2 · maxStep possible steps and 0 is not one of them. Every step picks one of these values with chance 1 / (2 · maxStep), independent of all earlier steps. For instance, when maxStep is 2 a step is one of -2, -1, 1 and 2.

The marker halts as soon as a step puts it distance or more away from 0, meaning at a position p with |p| >= distance. A step may carry it beyond that point (with distance 12 and maxStep 3, a step from 11 can land on 14); such a step also stops it.

Return how many steps the marker takes on average before it stops, counting the step that stops it.

Function

expectedExitSteps(maxStep: int, distance: int) → double

Examples

Example 1

maxStep = 3distance = 12return = 33.9398010834413

Each step is a nonzero whole number from -3 to 3. The exact mean is 1368347736/40316905, about 33.9398. A last step from 10 or 11 can land on 13 or 14; such a step also ends the walk.

Example 2

maxStep = 2distance = 2return = 2.0

From 0, a step of -2 or +2 ends the walk; from 1 (or -1), only the steps +1 and +2 (or -1 and -2) do. Writing a for the mean from 0 and b for the mean from 1: a = 1 + b/2 and b = 1 + (a + b)/4. So b = 2 and a = 2.

Example 3

maxStep = 1distance = 7return = 49.0

With steps of size 1 the walk cannot jump past 7, and the mean is 7 · 7 = 49.

Constraints

  • 1 <= maxStep <= 10
  • 1 <= distance <= 1000
  • Your result r is accepted if |r - expected| <= 10^-9 · expected; every answer is 1 or larger.

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
  • 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

  • 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 expectedExitSteps(int maxStep, int distance) {
        // Write your code here.
    }
}
Checking account…