Design a weighted random value generator
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of weighted random sampling, probability-proportional selection, data structures for aggregating weights, and API design with time-based expiration semantics.
Constraints
- pick operations provide a ticket instead of calling randomness.
- ticket is normalized into the active total weight.
- add_expiry uses the current logical time plus ttl as its expiration.
Examples
Input: ([["add",1,1],["add",2,1],["add",5,3],["pick",1],["pick",3],["pick",5]],)
Expected Output: [1, 5, 5]
Explanation: Tickets map deterministically across cumulative weights.
Input: ([["pick",1],["add","a",2],["add_expiry","b",3,10],["pick",4],["tick",10],["pick",3]],)
Expected Output: ['EMPTY', 'b', 'a']
Explanation: Expired entries stop contributing after their expiry time.
Input: ([["add","x",0],["pick",1]],)
Expected Output: ['EMPTY']
Explanation: No active positive weight returns EMPTY.
Hints
- Maintain entries in insertion order.
- For a pick, scan cumulative active weights until the ticket falls into a range.