KatInterview

Bankrupt Risk in Dice Game

Dynamic ProgrammingAuto-graded · 19 test cases
EasyAuto-gradedDynamic Programmingdynamic-programmingmath-bitsstatistics

Last 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