Probability a Draw-Until-Threshold Game Finishes at or Below a Score Cap

Quick Overview

Google new-grad onsite probability problem: a player draws uniform random integers from 1 to a maximum until the running total reaches a threshold. Compute the probability that the final total is at most a given cap, testing probability recurrences over many reachable totals within tight limits.

Probability a Draw-Until-Threshold Game Finishes at or Below a Score Cap

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A player starts with 0 points and draws numbers while their total is below `k`. Each draw adds an integer chosen uniformly at random from `1` to `max_pts`, independently of all earlier draws. As soon as the total reaches `k` or more, the player stops drawing. Return the probability that the player's final total is at most `n`. The report describes this round as a variant of a known probability task of this kind without saying what the variant changed; this version poses the base task. ### Function Signature ```python def probability_at_most(n: int, k: int, max_pts: int) -> float: ``` ### Rules - If `k == 0`, the player never draws, the final total is 0, and the answer is `1.0`. - Return the probability as a floating-point number. An answer within an absolute error of `10^-5` of the exact probability is accepted. ### Constraints - `0 <= k <= n <= 10^4` - `1 <= max_pts <= 10^4` ### Examples **Example 1** ```text Input: n = 10, k = 1, max_pts = 10 Output: 1.00000 ``` The player draws once, and every possible total from 1 to 10 is at most 10. **Example 2** ```text Input: n = 6, k = 1, max_pts = 10 Output: 0.60000 ``` The player draws once, and 6 of the 10 equally likely totals are at most 6. **Example 3** ```text Input: n = 21, k = 17, max_pts = 10 Output: 0.73278 ``` The output shows the exact probability rounded to five decimal places.

Overview: Google new-grad onsite probability problem: a player draws uniform random integers from 1 to a maximum until the running total reaches a threshold. Compute the probability that the final total is at most a given cap, testing probability recurrences over many reachable totals within tight limits.

Google logo
Google
Apr 3, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
2
0

A player starts with 0 points and draws numbers while their total is below k. Each draw adds an integer chosen uniformly at random from 1 to max_pts, independently of all earlier draws. As soon as the total reaches k or more, the player stops drawing. Return the probability that the player's final total is at most n.

The report describes this round as a variant of a known probability task of this kind without saying what the variant changed; this version poses the base task.

Function Signature

def probability_at_most(n: int, k: int, max_pts: int) -> float:

Rules

  • If k == 0 , the player never draws, the final total is 0, and the answer is 1.0 .
  • Return the probability as a floating-point number. An answer within an absolute error of 10^-5 of the exact probability is accepted.

Constraints

  • 0 <= k <= n <= 10^4
  • 1 <= max_pts <= 10^4

Examples

Example 1

Input:  n = 10, k = 1, max_pts = 10
Output: 1.00000

The player draws once, and every possible total from 1 to 10 is at most 10.

Example 2

Input:  n = 6, k = 1, max_pts = 10
Output: 0.60000

The player draws once, and 6 of the 10 equally likely totals are at most 6.

Example 3

Input:  n = 21, k = 17, max_pts = 10
Output: 0.73278

The output shows the exact probability rounded to five decimal places.