Skip to main content

Pattern 14: Probability & Expected Value

The Expectation Pattern: Calculate expected outcomes in probabilistic scenarios. Dice games, random walks, Markov chains.


The Core Pattern

THE MENTAL MODEL

Imagine calculating average outcome when rolling dice:

  1. Some events happen with probability < 1
  2. Calculate expected value = sum of (outcome × probability)
  3. State tracks probability or expected value at each position
  4. Combine using probability math: P(A and B) = P(A) × P(B)
  5. Use addition for expected values, multiplication for probabilities

This is Probability DP.

Pattern Recognition

WHEN YOU SEE THIS PATTERN

Keywords: "probability", "expected", "average", "random", "dice", "chances"

Structure:

  • Random/probabilistic events
  • Need expected value or probability
  • Combine probabilities from subproblems

State:

  • dp[i] = "probability of reaching state i"
  • OR dp[i] = "expected value from state i"

Transitions:

  • Probability: dp[i] = sum(dp[j] × P(j→i))
  • Expected value: dp[i] = sum(P(j) × value(j))

Common Problems

  • Knight Probability in Chessboard
  • Soup Servings
  • New 21 Game
  • Dice Roll Simulation
  • Airplane Seat Assignment Probability

(Example problems will be added here)