MEDIUMasked at 21 companies

Random Pick with Weight

A medium-tier problem at 48% community acceptance, tagged with Array, Math, Binary Search. Reported in interviews at Two Sigma and 20 others.

Founder's read

Random Pick with Weight is a medium difficulty problem that sits at the intersection of probability and data structures. You're given an array of weights and need to return an index randomly, but with probability proportional to each element's weight. It shows up across 21 companies including Netflix, Meta, and Two Sigma. The naive approach (generate random, iterate, subtract) works but tanks on repeated calls. The trick is prefix sums and binary search. Miss this pattern in your live assessment and you're stuck; nail it and you move on in 10 minutes. This is exactly where StealthCoder saves you if the pattern doesn't click.

Companies asking
21
Difficulty
MEDIUM
Acceptance
48%

Companies that ask "Random Pick with Weight"

If this hits your live OA

Random Pick with Weight is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.

Get StealthCoder
What this means

The core insight: build a prefix sum array, then a random number between 1 and total weight maps directly to an index via binary search. Most candidates start with a linear scan through weights on each call, which is correct but slow. The prefix sum transforms random selection into a lookup problem. You generate one random integer, binary search to find which weight range it falls into, and return that index. Binary Search, Prefix Sum, and Randomized all converge here. The second trap is off-by-one logic when mapping the random value to the correct index. If you freeze on the binary search boundary condition during your OA, StealthCoder surfaces the clean solution instantly, invisible to the proctor.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Random Pick with Weight recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Random Pick with Weight interview FAQ

Why can't I just iterate through weights on each random pick call?+

You can, but it's O(n) per call. With prefix sums and binary search, you drop to O(log n) per call. On repeated calls or large arrays, the difference is massive. Interviewers at Meta and Netflix will push back on linear scans.

How does the prefix sum actually map to random selection?+

Build an array where index i holds the cumulative sum up to that point. Generate a random number from 1 to total weight. Use binary search to find the smallest index whose prefix sum is greater than or equal to your random number. That's your pick.

Is this still asked at FAANG, or is it considered too classic?+

It shows up regularly. Two Sigma, Netflix, Meta, and Criteo all report it. It's not a trick question; it's a foundational pattern test. If you see it, it's a green flag that the company values clean data structure thinking.

What's the difference between this and a standard binary search problem?+

The randomization and probability framing add context, but algorithmically it's binary search on a sorted prefix sum array. The real work is recognizing that random selection fits into the search problem shape.

If I blank on binary search logic in the OA, how do I recover?+

That's when StealthCoder runs. It reads the problem from your screen, surfaces a working prefix sum and binary search solution in seconds, and you paste confident code. The proctor sees nothing. You move on.

Want the actual problem statement? View "Random Pick with Weight" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.