Categorical Sampling with Recorded Draws
Updated
Problem statement
For this exercise, a categorical distribution is represented by nonnegative integer weights. Category i has probability weights[i] / W, where W is the positive total weight.
Assign consecutive ticket ranges to categories in input order. Category i owns [sum(weights[0..i-1]), sum(weights[0..i])). A zero-weight category owns no ticket.
Each element of draws is an already-recorded uniform integer ticket from 0 through W-1. Return the category index containing each ticket, preserving draw order. Do not generate extra random numbers, sort the categories or alter their weights. An empty draw batch returns an empty array.
If the ticket is uniform, each category is selected with exactly its specified probability because its range contains exactly its weight in tickets.
Function
sampleCategories(weights: int[], draws: int[]) → int[]Examples
Example 1
weights = [2,3,1]draws = [0,1,2,4,5]return = [0,0,1,1,2]Category 0 owns tickets 0 and 1, category 1 owns 2 through 4, and category 2 owns 5.
Example 2
weights = [0,4,0,1]draws = [4,0,3]return = [3,1,1]Zero-weight categories own no ticket; ticket 4 belongs to category 3. Draw order is preserved.
Example 3
weights = [3]draws = []return = []An empty sequence of recorded draws produces no samples.
Constraints
1 <= weights.length <= 100000;0 <= draws.length <= 100000.0 <= weights[i] <= 1000000and1 <= W <= 1000000000.- Every draw satisfies
0 <= draws[q] < W. - Category indices are zero-based.
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Random Pick with Weight Using Recorded DrawsMeta · PHONE SCREENMeta interview questions
- Random Pick with Weight Using Recorded DrawsAmazon · ONSITE INTERVIEW
- Multinomial Sampling with Distribution ChecksNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Square Root FunctionCitadel · PHONE SCREENCitadel interview questions
- Convex Function MinimizationUber · PHONE SCREENUber interview questions
- Maintain a Consistent-Hashing RingApple · PHONE SCREENApple interview questions
- Square Root with Newton IterationGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Time Map with Sum and AverageConfluent · PHONE SCREENConfluent interview questions
- Good Ways to Split an ArraySalesforce · PHONE SCREENSalesforce interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)