The New 21 Game is a probability problem. A player starts with 0 points. Each round, they draw a number from 1 to maxPts uniformly at random and add it to their score. They stop when their score reaches at least n. Find the probability that their final score is at most maxScore. This New 21 Game coding problem combines probability reasoning with dynamic programming and a sliding window optimization.
Microsoft, Meta, Amazon, Google, and Bloomberg ask this because it requires translating a probability problem into DP with a clever sliding window optimization. Understanding how to express transition probabilities cleanly and then optimize the summation window is the key challenge. The math, sliding window, probability, and dynamic programming interview pattern is at the core.
DP with sliding window. dp[i] = probability of reaching exactly score i before stopping. For scores 0 to n-1 (still drawing), dp[i] = (sum of dp[i-maxPts..i-1]) / maxPts. For scores n to maxScore (stopped), accumulate them into the answer. Maintain a rolling window sum to avoid O(maxPts) inner computation per state, achieving O(n + maxScore) total time.
n=10, maxPts=10, maxScore=21. Start at 0. Can draw 1-10 each round. dp[0]=1. For i=1 to 9: dp[i] = windowSum/10. For i=10 to 21: these are stopped scores — add dp[i] to answer if i ≤ maxScore. Answer = sum of dp[n..maxScore] = probability ≈ 1.0 (most draws stay within 21 here, depends on parameters).
Probability DP problems translate "probability of event X" into dp[state] = P(reaching state) and compute transitions as weighted sums. The sliding window optimization here is the key performance insight — recognize when a DP transition is a sum over a fixed-size window and apply the +new/-old sliding window pattern. Practice this combination of probability reasoning + DP + sliding window as a compound skill — it appears in card game, lottery, and scoring problems across top-company interviews.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Soup Servings | Medium | Solve | |
| Airplane Seat Assignment Probability | Medium | Solve | |
| Toss Strange Coins | Medium | Solve | |
| 2 Keys Keyboard | Medium | Solve | |
| Integer Break | Medium | Solve |