Aggressive Wagering: Chance of Hitting the Goal Before Going Broke
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Model a state-dependent betting strategy as a Markov chain and solve absorption probabilities, including cycles, via a linear system.
A player holds some number of chips and wants to grow the pile to a fixed goal. The player repeatedly places a single bet, and each bet is settled independently.
Betting rule
If the player currently holds x chips (with 0 < x < goal), the wager is the largest amount that is still sensible:
- It cannot exceed the chips the player holds,
x. - It cannot exceed the chips still needed to reach the goal,
goal - x.
So the wager is min(x, goal - x).
Each bet is won with probability win_prob. A win adds the wagered amount to the pile; a loss removes it. Bets are independent.
Stopping
The game ends as soon as the pile reaches goal chips (success) or 0 chips (ruin). Since the wager never exceeds x or goal - x, the pile always stays between 0 and goal.
Task
Return the probability that the pile reaches goal before it reaches 0.
Edge-case rules:
- If
start >= goal, return1.0. - If
start <= 0, return0.0.
Note that the pile can return to a previously visited value, so a single path-product is not enough; all repeated visits must be accounted for.
Constraints
start,goalare integers;goalis small (at most a few dozen).0 <= win_prob <= 1.- Answers are compared as floating-point numbers with a small tolerance.
Examples
Example 1
Input: chance_of_reaching_goal(3, 5, 0.6)
Output: 0.7283531409168081
Hints
Hint 1
Define f(x) as the probability of eventually reaching the goal from x chips, with f(0)=0 and f(goal)=1. Condition on the result of the next bet.
Hint 2
Each non-terminal state gives one linear equation: f(x) = p*f(x+bet) + q*f(x-bet). Because states can repeat, you get a system of equations rather than a simple recursion.
Hint 3
Solve the (goal-1)-variable linear system with Gaussian elimination (partial pivoting), moving the known boundary value f(goal)=1 to the right-hand side.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat the pile size as a Markov chain. States 0 and goal are absorbing. For each in-between state x, the wager is b = min(x, goal - x), so the next state is x + b with probability p or x - b with probability q = 1 - p.
Let f(x) be the probability of reaching goal before 0 from x. Then f(0) = 0, f(goal) = 1, and for 0 < x < goal:
f(x) = p * f(x + b) + q * f(x - b)
Because the chain can revisit states (for example with goal 5: 3 -> 1 -> 2 -> 4 -> 3), these equations form a genuine linear system, not a simple recursion.
Building the system
There are goal - 1 unknowns. For row x, put 1 on the diagonal, subtract p at the column of the win state and q at the column of the loss state when they are transient, and add p to the right-hand side when the win state is exactly goal (a loss to 0 contributes nothing). Solve with Gauss-Jordan elimination with partial pivoting and read off the entry for start.
Why it is correct
The system (I - Q) f = r has a unique solution because from every transient state the goal or ruin is reached with probability 1 (for 0 < p < 1), so I - Q is invertible. The solution is exactly the absorption probability. The boundary rules for start >= goal and start <= 0 handle the trivial cases.
For start=3, goal=5, p=0.6, the closed form (p + q p^3) / (1 - p^2 q^2) = 429/589 ≈ 0.72835 matches.
Complexity
Time O(goal^3) for Gaussian elimination, space O(goal^2) for the matrix.
Reference solution
def chance_of_reaching_goal(start: int, goal: int, win_prob: float) -> float:
if start >= goal:
return 1.0
if start <= 0:
return 0.0
if goal <= 0:
return 1.0
n = goal - 1
lose_prob = 1.0 - win_prob
matrix = [[0.0] * (n + 1) for _ in range(n)]
for chips in range(1, goal):
row = chips - 1
matrix[row][row] = 1.0
bet = min(chips, goal - chips)
win_state = chips + bet
if win_state == goal:
matrix[row][n] += win_prob
else:
matrix[row][win_state - 1] -= win_prob
lose_state = chips - bet
if lose_state > 0:
matrix[row][lose_state - 1] -= lose_prob
for column in range(n):
pivot = max(range(column, n), key=lambda r: abs(matrix[r][column]))
if abs(matrix[pivot][column]) < 1e-15:
continue
matrix[column], matrix[pivot] = matrix[pivot], matrix[column]
divisor = matrix[column][column]
for j in range(column, n + 1):
matrix[column][j] /= divisor
for row in range(n):
if row == column:
continue
factor = matrix[row][column]
if factor == 0.0:
continue
for j in range(column, n + 1):
matrix[row][j] -= factor * matrix[column][j]
return matrix[start - 1][n]