FastPrepDice-Walk Winning Probability
Problems/Dice-Walk Winning Probability

Dice-Walk Winning Probability

Google logoGoogle● MediumNEW GRADPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reported dice-walk probability task matches at about 80%. Fair independent rolls, stopping on the first position at least mn, numeric bounds and floating tolerance are explicit practice assumptions.

Start at position 0 on an infinite one-dimensional grid. While the current position is less than mn, roll a fair k-sided die with faces 1 through k and move forward by the rolled value. Rolls are independent.

Stop immediately when the position is at least mn. Win if that stopping position is at most mx; otherwise lose. Return the exact winning probability as a floating-point value computed analytically, without Monte Carlo sampling.

Function

winningProbability(k: int, mn: int, mx: int) → double

Examples

Example 1

k = 10mn = 1mx = 6return = 0.6

The first roll ends the walk; six of the ten equally likely faces win.

Example 2

k = 2mn = 2mx = 2return = 0.75

Rolling 2 immediately wins with probability 1/2. Rolling 1 followed by 1 also wins, adding 1/4.

Example 3

k = 3mn = 0mx = 0return = 1.0

The walk starts in its stopping interval, so no roll is made.

Constraints

  • 1 <= k <= 10^4.
  • 0 <= mn <= mx <= 10^4.
  • An answer within absolute error 10^-9 is accepted.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public double winningProbability(int k, int mn, int mx) {
        // Write your code here.
    }
}
Checking account…