Implement weighted random sampling with preprocessing
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates algorithm design and probabilistic reasoning for weighted random sampling, including data-structure preprocessing, time-space trade-offs, expected versus worst-case performance, and attention to numerical precision.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ([1,3,2], [0,1,3,4,5])
Expected Output: [0, 1, 1, 2, 2]
Explanation: Draws map into prefix ranges.
Input: ([5], [0,4])
Expected Output: [0, 0]
Explanation: Single bucket.
Input: ([2,2,2], [0,2,4])
Expected Output: [0, 1, 2]
Explanation: Equal weights.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.