FastPrepNew 21 Game Probability

New 21 Game Probability

UiPath logoUiPath● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

A player starts with 0 points and repeatedly draws an integer uniformly at random from 1 through maxPts, inclusive. The player stops drawing as soon as the score is at least k.

Return the probability that the final score is at most n.

Function

new21Game(n: int, k: int, maxPts: int) → double

Examples

Example 1

n = 10k = 1maxPts = 10return = 1.0

Every possible first draw is at most 10.

Example 2

n = 6k = 1maxPts = 10return = 0.6

Exactly six of the ten equally likely first draws finish at 6 or below.

Example 3

n = 21k = 17maxPts = 10return = 0.7327777870686082

Dynamic programming accumulates the probability of every reachable stopping score from 17 through 21.

Constraints

  • 0 <= k <= n <= 10000
  • 1 <= maxPts <= 10000
  • The result is accepted with absolute tolerance 1e-9.

More UiPath problems

See UiPath hiring insights
public double new21Game(int n, int k, int maxPts) {
  // write your code here
}
n10
k1
maxPts10
expected1.0
Checking account…