Generate Uniform rand3 from rand10
Updated
Problem statement
A source function rand10() returns each integer from 0 through 9 with equal probability. Implement a sampler that returns each of 0, 1, and 2 with equal probability.
For this executable practice, the array draws records successive values returned by independent calls to rand10(). Consume values from left to right until your sampler produces one result, then return that result. Ignore all later values. The array always contains a value from 0 through 8, so a result is available.
Your result must be uniform when the draws are independent and uniform. A direct rand10() % 3 is biased because ten values do not divide evenly into three groups.
Function
rand3FromRand10(draws: int[]) → intExamples
Example 1
draws = [0]return = 0The first draw is accepted and maps to output 0.
Example 2
draws = [9,8]return = 2Reject 9. The next draw is 8, which maps to output 2.
Example 3
draws = [9,9,3,2]return = 0Reject both leading nines. Draw 3 maps to 0; the later 2 is unused.
Constraints
1 <= draws.length <= 100.0 <= draws[i] <= 9.- At least one element of
drawsis less than9. - The array stands in for one run of the sampler. In the real setting, each
rand10()call is independent and uniform.
More NVIDIA Online Assessment questions
- Query a Header-Encoded MatrixOA · Seen Oct 2026
- Maximum ScoreOA · Seen Oct 2026
- Address on Same PageOA · Seen Oct 2026
- Minimize Task Resource ConsumptionOA · Seen Oct 2026
- Minimum Project Bid CostOA · Seen Oct 2026
- Count Ideal Numbers in a RangeOA · Seen Oct 2026
- Generate Bit MaskOA · Seen Oct 2026
- Reconstruct an Array from Prefix XOROA · Seen Oct 2026
- Sum of Subarray MinimumsOA · Seen Oct 2026
- Unique Integers That Sum to ZeroOA · Seen Oct 2026
Similar questions at other companies
- Bold Play to Reach a GoalSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Chance a Die Game Balance Reaches ZeroOptiver · OAOptiver interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Sliding-Window Means with IEEE Special ValuesHeadlands Technologies · OAHeadlands Technologies interview questions
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Polynomial CoefficientsShopify · OAShopify interview questions
- Basic Calculator IITypeSafe AI · OA
- Last Object to Fall Off the TableConfluent · OAConfluent interview questions
- Last Object Exit TimeIBM · OAIBM interview questions
- Count Prime Values in SubtreesVisa · OAVisa interview questions