Random walk absorption

Write codeHard35 minCitadel and Goldman Sachs style

Your task

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).)

Examples

expected_steps_to_absorb(n: int, s: int) -> float
Example 1
Inputexpected_steps_to_absorb(3, 1)
Output1