Multinomial Sampling with Distribution Checks
Updated
Problem statement
Implement sampling from a list of category probabilities. For this exercise, assume probabilities are represented by nonnegative integer weights: category i has probability weights[i] / W, where W is the positive sum of all weights.
The task supplies the integer draws in tickets. Each ticket is in [0, W). Return one zero-based category index for each ticket, in the same order. Do not generate new random values or sort the tickets.
For this exercise, assume categories own consecutive half-open ranges in their original order. If S_i is the sum of weights before category i, its range is [S_i, S_i + weights[i]). A zero-weight category owns no tickets. A ticket exactly at a range’s upper boundary belongs to the next nonempty range.
If the supplied draws are independent and uniform on [0, W), each returned category has the requested probability. A histogram of m such draws is a multinomial count vector. Valid tickets need not themselves look uniformly distributed; the returned indices must always obey the mapping.
Distribution-check follow-up
Implement the separate helper maxEmpiricalDeviation(weights, samples) for a nonempty array of valid sampled category indices. For this exercise, assume it returns max_i(abs(count_i / samples.length - weights[i] / W)), using double precision. Include every category, even one with zero weight. Your main function is the sampler. This helper is a follow-up, and the editorial has reference code for it.
For example, weights [1,0,3] and samples [0,2,2,2] give deviation 0. Samples [0,0,2,2] give deviation 0.25. Finite random samples normally differ from the target. This metric measures a discrepancy; it does not prove a generator is correct or provide a statistical confidence level.
Notebook benchmark follow-up
Run experiments on how to make the program faster, especially moving random-number generation outside the loop and replacing a list with a NumPy array; changing linear search to binary search is not the focus.
- Compare generating one ticket per iteration with generating the same number of tickets in one batch.
- With a common draw array and the same cumulative masses, compare Python list mapping, array mapping in a Python loop, and vectorized array mapping. Check that outputs agree before timing them.
- Measure preprocessing, random-number generation, mapping and the complete pipeline separately. Repeat runs, report the environment and sample count, and explain allocations and memory use.
- Compute a histogram and the empirical discrepancy. Explain why no particular finite random sample must match the probabilities exactly.
For this exercise, treat these timing experiments as open discussion. There is no required speedup and no time target to hit.
Function
sampleCategories(weights: int[], tickets: int[]) → int[]Examples
Example 1
weights = [1,0,3]tickets = [0,1,2,3]return = [0,2,2,2]Category 0 owns [0,1), category 1 owns no tickets, and category 2 owns [1,4). The histogram is [1,0,3].
Example 2
weights = [5]tickets = [0,4,1]return = [0,0,0]The only category owns all five possible tickets. Ticket order is preserved.
Example 3
weights = [0,2,0,0,3,0]tickets = [4,0,2,1,3]return = [4,1,4,1,4]Only categories 1 and 4 have mass. Ticket 2 is the boundary after category 1 and belongs to category 4; zero-weight categories are skipped.
Constraints
1 <= weights.length <= 1000.0 <= weights[i] <= 10^6and1 <= W <= 10^9.1 <= tickets.length <= 1000and0 <= tickets[j] < W.- For the optional checker,
1 <= samples.length <= 1000and0 <= samples[j] < weights.length.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Random Pick with Weight Using Recorded DrawsAmazon · ONSITE INTERVIEW
- Random Pick with Weight Using Recorded DrawsMeta · PHONE SCREENMeta interview questions
- Totals from a Tiered Rate TableMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Koko Eating BananasTekion · ONSITE INTERVIEWTekion interview questions
- Farthest Affordable Shop per QueryAdobe · ONSITE INTERVIEWAdobe interview questions
- Weighted Random Key SelectionRoblox · ONSITE INTERVIEWRoblox interview questions
- Shortest Subarray with Sum at Least KOracle · ONSITE INTERVIEWOracle interview questions
- Roll a Weighted DieLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions