FastPrepGenerate Uniform rand3 from rand10
Problems/Generate Uniform rand3 from rand10

Generate Uniform rand3 from rand10

NVIDIA logoNVIDIA● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an NVIDIA online assessment for new grads. The report asks for a uniform rand3() built from a uniform rand10() that returns 0 through 9. It leaves out the exact function interface, the conditions for independence and for stopping, and how it is graded. Here we supply recorded draws and guarantee that a draw is accepted.

Our version is about 70% the same as the reported question.

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[]) → int

Examples

Example 1

draws = [0]return = 0

The first draw is accepted and maps to output 0.

Example 2

draws = [9,8]return = 2

Reject 9. The next draw is 8, which maps to output 2.

Example 3

draws = [9,9,3,2]return = 0

Reject 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 draws is less than 9.
  • 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

Interview reports that mention this question

  • NVIDIA OA: What It Tests (2026)

NVIDIA interview experiences

  • NVIDIA Full-Time SWE Onsite Scheduling: 1 Experience
  • NVIDIA DevOps, QA and Infra Intern Interviews: 12 Experiences
  • NVIDIA Ignite Internship OA Experience
  • NVIDIA Software Intern Interview Questions: 9 Experiences
  • NVIDIA Ignite SWE Intern OA Questions: 6 Experiences
All NVIDIA interview questionsSee NVIDIA hiring insights
Saved
class Solution {
    public int rand3FromRand10(int[] draws) {
        // Write your code here.
    }
}
Checking account…