Random walk absorption
You have a row of n cells indexed 0..n-1. A token starts at cell s. Each step it moves left or right by one cell, each with probability 1/2, EXCEPT at the two ends which are absorbing: cell 0 and cell n-1 stop the walk. Return the expected number of steps until absorption, rounded to 6 decimal places. Constraints: 2 <= n <= 100000, 0 <= s <= n-1. (For an interior start the answer has the closed form s*(n-1-s).)
Implement
expected_steps_to_absorb(n: int, s: int) → floatExamples
in
[3,1]out1What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 35 min
solution.py
InputExpectedGot
[3,1]1not run yetsample