Design playlist with add/remove/shuffle
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates data structure design and algorithmic complexity skills, focusing on managing dynamic sets, uniform random selection, and average-case time/space trade-offs.
Constraints
- 0 <= len(operations) <= 200000
- len(operations) == len(values)
- Each operation is one of: 'add', 'remove', 'shuffle'
- For 'add' and 'remove', songId is an integer in the range [-10^9, 10^9]
- For 'shuffle', the corresponding value is None
- 0 <= seed < 2^31
Examples
Input: (["add", "add", "shuffle", "remove", "shuffle"], [10, 20, None, 10, None], 1)
Expected Output: [True, True, 10, True, 20]
Explanation: After adding 10 and 20, the first shuffle picks index 0 and returns 10. Removing 10 leaves only 20, so the second shuffle returns 20.
Input: (["shuffle", "add", "add", "remove", "shuffle"], [None, -5, -5, 7, None], 42)
Expected Output: [None, True, False, False, -5]
Explanation: The first shuffle is on an empty playlist, so it returns None. Adding -5 succeeds, adding it again fails, removing 7 fails, and the final shuffle returns -5.
Input: (["add", "add", "add", "remove", "shuffle", "remove", "shuffle", "add", "shuffle"], [7, 8, 9, 8, None, 7, None, 10, None], 1)
Expected Output: [True, True, True, True, 7, True, 9, True, 9]
Explanation: Removing 8 swaps in the last song, so the playlist becomes [7, 9]. The next shuffle returns 7. Removing 7 leaves [9], then adding 10 creates [9, 10], and the last shuffle returns 9.
Input: (["add", "add", "shuffle", "shuffle", "remove", "add", "shuffle", "shuffle"], [100, 200, None, None, 100, 300, None, None], 1)
Expected Output: [True, True, 100, 200, True, True, 200, 300]
Explanation: Two songs are added. The first two shuffles return 100 and 200 based on the deterministic state updates. After removing 100 and adding 300, the remaining shuffles return 200 and then 300.
Hints
- You need fast membership checks and fast access to a song's current index.
- Removing from the middle of an array is expensive; consider swapping the target with the last element before popping.