Opening scenario
Random Pick with Weight (LC 528). You get an array
wof positive integer weights.pickIndex()must return indexiwith probabilityw[i] / sum(w). Then the follow-up: Implement Rand10() Using Rand7() (LC 470). Build a uniformrand10()when your only random source isrand7().
LC numbers identify LeetCode problems.
Start small. Three items A, B and C must come back with probabilities 0.2, 0.3 and 0.5. Your only tool is random.random(), a uniform float in [0, 1). How do you turn one uniform number into this skewed choice? And how do you prove that the result is exact?
Interviewers use this problem family to check three skills:
- Construct: turn a known source, usually a uniform one, into the target distribution.
- Prove: show that each outcome has exactly the required probability.
- Optimize: cut the time, the memory or the number of random calls per sample.
The proof step usually separates strong candidates from the rest.
In the full tutorial
- Question progression
- Canonical Template
- Complexity
- Pattern Recognition Signals
- Practice Set
- Pitfalls
- Variations
- Closing: When to Reach for This