Implement rotating homepage title selector
Company: Netflix
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
Quick Answer: This question evaluates understanding of data structures and algorithms for prioritized selection with stateful constraints, focusing on maintaining dynamic relevance scores and avoiding immediate repeats; it is categorized under Coding & Algorithms.
Constraints
- 1 <= len(operations) <= 200000
- title_id consists of printable non-empty strings with length <= 50
- -10^9 <= score <= 10^9
- There are no delete operations
- Your solution should support many repeated updates to the same title efficiently
Examples
Input: [('upsert', 'movieA', 10.0), ('upsert', 'movieB', 9.0), ('get',), ('get',), ('get',), ('get',)]
Expected Output: ['movieA', 'movieB', 'movieA', 'movieB']
Explanation: movieA has the highest score, but once it is shown, movieB is chosen next to avoid repeating the same title consecutively.
Input: [('upsert', 'a', 5.0), ('upsert', 'b', 4.0), ('get',), ('upsert', 'b', 6.0), ('get',), ('get',), ('upsert', 'a', 7.0), ('get',), ('get',)]
Expected Output: ['a', 'b', 'a', 'b', 'a']
Explanation: Updates immediately change the ordering. Even after a title becomes highest-scored, it is skipped if it was also the previous answer and another title exists.
Input: [('upsert', 'solo', 3.5), ('get',), ('get',), ('upsert', 'solo', 8.0), ('get',)]
Expected Output: ['solo', 'solo', 'solo']
Explanation: With only one title in the system, it may be returned on consecutive calls.
Input: [('upsert', 'beta', 5.0), ('upsert', 'alpha', 5.0), ('get',), ('get',), ('get',)]
Expected Output: ['alpha', 'beta', 'alpha']
Explanation: The scores are tied, so the lexicographically smaller title_id is preferred among eligible titles.
Input: [('get',), ('upsert', 'x', -1.0), ('get',)]
Expected Output: [None, 'x']
Explanation: A get on an empty system returns None. Negative scores are still valid and can be returned if they are the only title.
Input: [('upsert', 'a', 10.0), ('upsert', 'a', 1.0), ('upsert', 'b', 5.0), ('get',), ('get',)]
Expected Output: ['b', 'a']
Explanation: The old score 10.0 for title 'a' must be ignored after it is updated to 1.0. Then 'b' is top, followed by 'a' on the next call to avoid repeating 'b'.
Hints
- You only need the best title, and sometimes the second-best title. A max-heap plus the previously returned title is a good starting point.
- Updates can leave old heap entries behind. Consider lazy deletion with version numbers so outdated scores are ignored.