Compute winning probability on 1D dice walk
Company: Google
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of probability theory and discrete stochastic processes, specifically reasoning about random walks and event probabilities over (potentially infinite) state spaces.
Constraints
- 0 <= mn <= mx <= 100000
- 1 <= k <= 100000
- Each die outcome from 1 to k is equally likely
Examples
Input: (3, 3, 2)
Expected Output: 0.625
Explanation: You win only if the stopping position is exactly 3. Winning roll sequences are [1,1,1], [1,2], and [2,1], with total probability 1/8 + 1/4 + 1/4 = 5/8 = 0.625.
Input: (1, 2, 3)
Expected Output: 0.6666666666666666
Explanation: The game stops after the first roll because mn = 1. You win if the die shows 1 or 2, so the probability is 2/3.
Input: (2, 2, 3)
Expected Output: 0.4444444444444444
Explanation: You win only by stopping exactly at 2. This happens with rolls [2] or [1,1], so the probability is 1/3 + 1/9 = 4/9.
Input: (0, 0, 6)
Expected Output: 1.0
Explanation: You start at position 0, and since 0 >= mn, the game is already over. Position 0 is inside [0, 0], so you win with probability 1.
Input: (6, 10, 5)
Expected Output: 1.0
Explanation: Just before the last roll, your position is at most 5. After adding a value from 1 to 5, the final position must lie in [6, 10], which is entirely inside the winning range.
Hints
- Let dp[s] represent the probability of reaching total s while the game is still allowed to continue. Each state receives probability from the previous k states.
- A naive DP would sum k values for every position. Use a sliding window to maintain the sum of the last k active probabilities in O(1) per state.