Gambler's Ruin: Chance of Hitting the Goal Before Bust
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Compute the probability that a biased ±1 random walk reaches a target balance before hitting zero, using a recurrence and closed form with careful edge cases.
A player begins with start units of money and repeatedly plays a round of a betting game. In each round, a weighted coin is tossed:
- With probability
pit shows heads, and the player gains exactly 1 unit. - With probability
1 - pit shows tails, and the player loses exactly 1 unit.
Rounds are independent, and p never changes. Play stops the moment the player's balance equals 0 (bust) or target (goal).
Write a function that returns the probability that the player reaches target before reaching 0.
Special cases
Apply these rules in this order before using any general formula:
- If
start <= 0, the player is already bust. Return0.0. - If
start >= target, the goal is already met. Return1.0. - If
p == 0.0, return0.0. - If
p == 1.0, return1.0. - If
p == 0.5, the game is fair. Returnstart / target.
Otherwise, compute the probability for the general biased case.
Input
start: integer, the starting balance.target: integer, the balance at which the player stops and wins.p: float, the probability of winning a single round.
Output
A float: the probability of reaching target before 0.
Constraints
startandtargetare integers.0.0 <= p <= 1.0.pstays constant during the game.- The game ends immediately upon reaching
0ortarget. - Results are compared as floating-point numbers, so use the standard closed-form expression rather than simulation.
Examples
Example 1
Input: reach_goal_probability(10, 20, 0.6)
Output: 0.9829540725450702
Example 2
Input: reach_goal_probability(10, 20, 0.5)
Output: 0.5
Example 3
Input: reach_goal_probability(3, 10, 0.4)
Output: 0.04191296854803968
Hints
Hint 1
Let f(x) be the chance of success from balance x. Condition on the first round to relate f(x) to f(x+1) and f(x-1), with f(0) = 0 and f(target) = 1.
Hint 2
Rewrite the recurrence in terms of differences f(x+1) - f(x). Each difference is the previous one multiplied by r = (1-p)/p, so the values form a geometric series.
Hint 3
Summing the geometric series and normalizing so that f(target) = 1 gives (1 - r^start) / (1 - r^target). This breaks down when r = 1 (p = 0.5), so handle that case separately with start / target.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Let f(x) be the probability of reaching target before 0 when the current balance is x, and let q = 1 - p.
Looking at the very next round, for 0 < x < target:
f(x) = p * f(x + 1) + q * f(x - 1)
The boundary values are f(0) = 0 and f(target) = 1.
Since f(x) = (p + q) f(x), the recurrence can be rearranged to:
f(x + 1) - f(x) = r * (f(x) - f(x - 1)), where r = q / p
So the successive differences form a geometric sequence. With d = f(1) - f(0) = f(1), we get f(x + 1) - f(x) = d * r^x. Summing from 0 to x - 1 gives:
f(x) = d * (1 - r^x) / (1 - r) (for r != 1)
Imposing f(target) = 1 determines d, and the result simplifies to:
f(x) = (1 - r^x) / (1 - r^target)
When p = 0.5, r = 1 and the differences are all equal. Then f is linear, and the boundaries force f(x) = x / target.
The cases p = 0 and p = 1 are handled directly (the walk only goes down or only goes up), which also avoids division by zero when computing r. The cases start <= 0 and start >= target are the already-finished games.
Why it is correct
The recurrence plus the two boundary values has a unique solution. The closed form satisfies both the recurrence and the boundaries, and the game ends with probability 1 when 0 < p < 1, so this is the required probability. As a sanity check, with p > 0.5 the answer exceeds start / target, and with p < 0.5 it is below it.
Complexity
- Time:
O(log(start) + log(target))for the exponentiations, which is effectivelyO(1). - Space:
O(1).
Reference solution
def reach_goal_probability(start, target, p):
if start <= 0:
return 0.0
if start >= target:
return 1.0
if p == 0.0:
return 0.0
if p == 1.0:
return 1.0
if p == 0.5:
return start / target
ratio = (1.0 - p) / p
return (1.0 - ratio ** start) / (1.0 - ratio ** target)