Expected Steps Until a Random Walk Gets Far From Zero
Updated
Problem statement
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) → doubleExamples
Example 1
maxStep = 3distance = 12return = 33.9398010834413Each 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.0From 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.0With steps of size 1 the walk cannot jump past 7, and the mean is 7 · 7 = 49.
Constraints
1 <= maxStep <= 101 <= distance <= 1000- Your result
ris 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