Chance a Sum of Random Draws Has a Given Number of Digits
Updated
Problem statement
You make count draws. Each draw picks one whole number from low, low + 1, ..., high (both ends included), every one of them with chance 1 / (high - low + 1), and independently of the other draws. So the same number may come up more than once.
Add up all the draws. Find the chance that this total, written in base 10, has exactly digits digits, meaning 10^(digits - 1) <= total <= 10^digits - 1. A total that is too large, with more than digits digits, does not count.
Function
probabilitySumHasDigits(count: int, low: int, high: int, digits: int) → doubleExamples
Example 1
count = 4low = 10high = 99digits = 3return = 0.9909211248285322The total is between 40 and 396, so three digits means a total of 100 or more. Take 10 off each draw: the four reduced values are 0 to 89 and the total is at most 99 only when they add up to 59 or less. No single value can pass 89 then, so there are C(63, 4) = 595,665 such outcomes out of 90^4 = 65,610,000. That leaves 1 - 595,665 / 65,610,000 = 1,444,763 / 1,458,000, about 0.9909211.
Example 2
count = 2low = 4high = 6digits = 1return = 0.3333333333333333Two draws from 4, 5, 6 give 9 equally likely pairs. Totals 8, 9, 10, 11, 12 come from 1, 2, 3, 2, 1 pairs. Only 8 and 9 have one digit, which gives 3 / 9 = 1/3.
Example 3
count = 1low = 5high = 14digits = 1return = 0.5One draw from the 10 values 5 to 14. The values 5 to 9 have one digit: 5 of 10, so 0.5.
Constraints
1 <= count <= 501 <= low <= high <= 10001 <= digits <= 9- The checker allows an absolute error of
10^-9.
More Optiver Online Assessment questions
- Chance a Die Game Balance Reaches ZeroOA · Seen Dec 2026
- Chance a Hand of Cards Multiplies to More Than a ThresholdOA · Seen Dec 2026
- Expected Draws to Remove All Green CountersOA · Seen Dec 2026
- Expected Profit of a Doubling Betting SystemOA · Seen Dec 2026
- Expected Steps Until a Random Walk Gets Far From ZeroOA · Seen Dec 2026
- Rolling Until One Face's Own Total Passes a ThresholdOA · Seen Dec 2026
- Two Ranked Teams Meeting in a Random BracketOA · Seen Dec 2026
- Overheat Prevention ControllerOA · Seen Nov 2026
- Power Cell BankOA · Seen Nov 2026
- Count Straight-Line Word Occurrences in a GridOA · Seen Sep 2026
Similar questions at other companies
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Count Lucky TicketsThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Jump Game with Prime-Step RuleUber · OAUber interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Unique Paths Through a GridPeopleStrong · OAPeopleStrong interview questions
- Get Minimum OperationsJP Morgan Chase · OAJP Morgan Chase interview questions
- Jump Game with Prime-3 StepsAgoda · OAAgoda interview questions
- Find Encrypted NumberAmazon · OA
- Prime StringSnowflake · OASnowflake interview questions