Bankrupt Risk in Dice Game
Dynamic ProgrammingAuto-graded · 19 test casesLast updated
Compute the exact probability of wealth hitting zero or below at any step during a finite sequence of random dice rolls.
Consider a game where you begin with a certain amount of cash, $W_0 = \text{initial_wealth}$. You participate in a game consisting of $k$ rounds.
In each round, a single fair six-sided die is rolled:
- Rolling an odd number ($1, 3, 5$) results in a loss equal to the face value ($-1, -3, -5$).
- Rolling an even number ($2, 4, 6$) results in a gain equal to the face value ($+2, +4, +6$).
If your current balance reaches $0$ or falls below $0$ at any point during or after a round, you go bankrupt immediately and the game ends for you.
Given the starting wealth initial_wealth and the maximum number of rounds k, calculate the exact probability that you experience bankruptcy at any point on or before the $k$-th round.
Constraints
- $1 \le \text{initial_wealth} \le 100$
- $0 \le k \le 50$
- Return the exact probability as a
float.
Edge Cases
- If $\text{initial_wealth} \le 0$, the probability of being bankrupt is $1.0$.
- If $k = 0$ and $\text{initial_wealth} > 0$, the probability of bankruptcy is $0.0$.
Examples
Example 1
Input: compute_ruin_probability(10, 10)
Output: 0.2351405188910905
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We are asked to compute the cumulative probability of hitting a balance $\le 0$ within $k$ steps. Since $k$ is relatively small ($k \le 50$), we can use dynamic programming or state-space probability tracking.
- State representation: Maintain a hash map (or array) mapping
current_wealth -> probabilityfor all reachable positive balances at step $t$. - Initial state: At step 0,
state_probs = {initial_wealth: 1.0}andruin_probability = 0.0. - Transitions: At each round from $1$ to $k$:
- For each active positive balance and its associated probability $P$, each of the 6 outcomes has probability $P / 6$.
- The 6 possible changes to wealth are $\Delta \in {-1, -3, -5, +2, +4, +6}$.
- If
wealth + delta <= 0, add $P / 6$ directly toruin_probability(absorbing state). - Otherwise, add $P / 6$ to the next round's probability map for key
wealth + delta.
- After repeating this for $k$ steps, return
ruin_probability.
Complexity Analysis
- Time Complexity: $\mathcal{O}(k \cdot S)$, where $S$ is the maximum number of reachable positive wealth states at step $t$. Since wealth changes by at most $+6$ or $-5$ per step, $S \le 11t + 1$. Thus, the total time complexity is $\mathcal{O}(k^2)$, which takes less than a few milliseconds for $k = 50$.
- Space Complexity: $\mathcal{O}(k)$ to store the active wealth states and their probabilities for the current and next steps.
Python Reference Solution
def compute_ruin_probability(initial_wealth: int, k: int) -> float:
if initial_wealth <= 0:
return 1.0
if k <= 0:
return 0.0
changes = [-1, -3, -5, 2, 4, 6]
state_probs = {initial_wealth: 1.0}
ruin_probability = 0.0
for _ in range(k):
next_probs = {}
for wealth, prob in state_probs.items():
step_prob = prob / 6.0
for delta in changes:
new_wealth = wealth + delta
if new_wealth <= 0:
ruin_probability += step_prob
else:
next_probs[new_wealth] = next_probs.get(new_wealth, 0.0) + step_prob
state_probs = next_probs
if not state_probs:
break
return ruin_probability