Random Pick with Weight Using Recorded Draws
Updated
Problem statement
You are given an array weights of positive integers and a sequence draws of recorded uniform random tickets. The weight at index i determines how many tickets select that index.
Let total be the sum of all weights. Tickets are integers from 0 through total - 1. Assign the first weights[0] tickets to index 0, the next weights[1] tickets to index 1, and so on. For each ticket, return its selected index. Keep the answers in the same order as draws.
The weights do not change between draws. If a ticket is sampled uniformly from this range, index i is selected with probability weights[i] / total. The input supplies each sampled ticket so the expected result is reproducible.
Function
pickIndices(weights: int[], draws: int[]) → int[]Examples
Example 1
weights = [1,3]draws = [0,1,2,3]return = [0,1,1,1]Ticket 0 belongs to index 0; tickets 1, 2, and 3 belong to index 1.
Example 2
weights = [2,1,2]draws = [0,1,2,3,4]return = [0,0,1,2,2]The half-open ticket intervals are [0,2), [2,3), and [3,5).
Example 3
weights = [5]draws = [4,0,2]return = [0,0,0]Every valid ticket chooses the only index.
Constraints
1 <= weights.length <= 2000.1 <= weights[i] <= 10^5.1 <= draws.length <= 2000.- For every ticket
tindraws,0 <= t < total, wheretotalis the sum of all weights. total <= 2 * 10^8, so cumulative weights fit in a signed 32-bit integer.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE 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 DrawsAmazon · ONSITE INTERVIEW
- Multinomial Sampling with Distribution ChecksNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Good Ways to Split an ArraySalesforce · PHONE SCREENSalesforce interview questions
- Minimum of a Convex FunctionRippling · PHONE SCREENRippling interview questions
- Two Non-Overlapping Increasing BlocksAdobe · PHONE SCREENAdobe interview questions
- Percentile and Boundary QueriesDatadog · PHONE SCREENDatadog interview questions
- Longest Run of Ones After Flipping k ZerosC3.ai · PHONE SCREENC3.ai interview questions
- Categorical Sampling with Recorded DrawsMicrosoft · PHONE SCREENMicrosoft interview questions
- Count Recorded Events in Time IntervalsSnowflake · PHONE SCREENSnowflake interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences