Random Pick with Weight Using Recorded Draws
Updated
Problem statement
You are given an array weights of positive integers and a list draws of tickets that were drawn at random and recorded. The weight at index i says how many tickets pick 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 every ticket in this range is equally likely, index i is picked with probability weights[i] / total. The input gives you each drawn ticket, so you never generate random numbers yourself.
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 picks index 0. Tickets 1, 2, and 3 each pick index 1. The draws come in that same order, so the answer is those indices in turn.
Example 2
weights = [2,1,2]draws = [0,1,2,3,4]return = [0,0,1,2,2]The first weight covers tickets from 0 up to, but not including, 2. The second weight covers tickets from 2 up to, but not including, 3. The third weight covers tickets from 3 up to, but not including, 5. So, in order, the draws pick the first, first, second, third, and third index.
Example 3
weights = [5]draws = [4,0,2]return = [0,0,0]There is only one weight, so every ticket from zero through four belongs to index zero. All three draws pick index zero, so every entry of the answer is zero.
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 Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Multinomial Sampling with Distribution ChecksNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- 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
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)