Skip to content
LeetQuant
QD01 Data Structures and Algorithms

QD01 · Data Structures and Algorithms · Lesson 2 of 7

Dynamic Programming for Probability Problems

Turning recursive probability and game questions into tables: distributions of sums, runs, and optimal stopping.

25 min

The recipe

Dynamic programming solves a problem by solving smaller versions of it and storing the answers. For probability and game questions:

  1. Define the state: the smallest description of the situation that determines the future (rolls left, current run length, current total).
  2. Write the recurrence: the value of a state in terms of the states it can move to, from first-step analysis.
  3. Fix the base cases and the order in which to fill the table so every value you need is ready.
  4. State the complexity: number of states times work per state.

Most "simulate it" questions in quant coding rounds have an exact DP answer, and interviewers prefer it.

Worked example

a run of five heads

What is the probability that 100 fair coin flips contain a run of at least five heads?

Show solution

The state is the length of the current run of heads (0 to 4), plus an absorbed "done" state. Each flip moves run length rr to r+1r + 1 (heads) or 00 (tails), and reaching 5 is absorbed.

def p_run(n: int = 100, k: int = 5) -> float:
    dist = [1.0] + [0.0] * (k - 1)  # P(current run = r, no run of k yet)
    done = 0.0
    for _ in range(n):
        new = [0.0] * k
        for r, p in enumerate(dist):
            new[0] += p / 2          # tails resets the run
            if r + 1 == k:
                done += p / 2        # heads completes the run
            else:
                new[r + 1] += p / 2
        dist = new
    return done

This returns about 0.8100.810, in O(nk)O(nk) time. Most people guess far lower: long runs are more common than intuition suggests.

Distributions by convolution

The distribution of a sum of independent pieces builds up one piece at a time: if fm(s)f_m(s) is the probability that the first mm dice sum to ss, then

fm+1(s)=16∑j=16fm(s−j).f_{m+1}(s) = \frac16\sum_{j=1}^{6} f_m(s - j) .

This takes O(n⋅6n⋅6)=O(n2)O(n \cdot 6n \cdot 6) = O(n^2) for nn dice instead of O(6n)O(6^n) enumeration. The same pattern counts paths on grids, ways to make change, and ways to climb stairs.

Optimal stopping by backward induction

With kk rolls left, the value VkV_k is the expected payoff under optimal play. On the last roll you must accept: V1=E[X]V_1 = \mathbb{E}[X]. With more rolls you keep a roll xx only if it beats continuing:

Vk+1=E[max⁡(X,Vk)].V_{k+1} = \mathbb{E}\big[\max(X, V_k)\big] .

For infinite-horizon versions with a cost per roll, the value VV satisfies V=E[max⁡(X,V−c)]V = \mathbb{E}[\max(X, V - c)] and the optimal rule is a threshold; solve for it directly or iterate until the value converges.

Worked example

a ten-sided die with two rolls

You roll a fair 10-sided die (faces 1–10) and may roll once more, taking the second result instead. What is the game worth?

Show solution

V1=5.5V_1 = 5.5. Keep the first roll if it is 6 or more:

V2=6+7+8+9+1010+510×5.5=4+2.75=6.75.V_2 = \frac{6 + 7 + 8 + 9 + 10}{10} + \frac{5}{10} \times 5.5 = 4 + 2.75 = 6.75 .

Knowledge check

In how many ways can you climb 10 stairs taking 1 or 2 steps at a time?

Knowledge check

A fair 6-sided die game allows up to two rolls; you may keep the first or take the second. What threshold should the first roll beat to keep it?

Knowledge check

What is the time complexity of computing the full distribution of the sum of n dice by convolution?

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).