Reason About a Movie Billboard Rotation Component
NetflixSoftware Engineer · OnsiteLast updated
This problem tests the ability to manage dynamic scheduling and priority updates using balanced binary search trees (ordered maps) while handling concurrency and system scalability.
Movie Billboard Rotation Component
You are tasked with building a billboard rotation engine that selects the next movie title to display based on a combination of a 'priority score' and 'last displayed timestamp.'
Instead of a heap, you must maintain state using two ordered maps:
PriorityMap: Maps(priority_score, movie_id)tomovie_metadata.TimestampMap: Maps(last_displayed_timestamp, movie_id)tomovie_metadata.
Requirements
Implement a class BillboardManager with the following interface:
class BillboardManager:
def add_movie(self, movie_id, priority):
# Adds a new movie with initial priority and timestamp 0
pass
def get_next_movie(self):
# Returns the movie_id that should be displayed next.
# Selection logic: prioritize highest priority, then oldest timestamp.
pass
def update_display(self, movie_id):
# Updates the movie's last_displayed_timestamp to the current system time
# and refreshes its position in the internal maps.
pass
Constraints
movie_idis a unique string.priorityis a float.- Ensure
update_displayoperations maintain the invariants of both ordered maps. - Time complexity for
get_next_movieandupdate_displayshould ideally beO(log N).
Example
- Initial state:
Movie A (p=10),Movie B (p=5). get_next_movie()returnsA(highest priority).update_display('A')sets A's timestamp toT1.- A subsequent
get_next_movie()call must consider the updated timestamp for future tie-breaking if priorities are identical.
Hints
Hint 1
To achieve O(log N) updates, ensure you remove and re-insert the specific key-value pair in both maps whenever a movie's metadata changes.
Hint 2
The 'selection' logic should prioritize the primary key (score) and use the secondary key (timestamp) to break ties or influence frequency capping.
Hint 3
Think of the two maps as two different indices on the same set of data; any mutation must be atomic across both structures to prevent inconsistencies.
Follow-up questions
- How would you ensure thread safety when multiple workers concurrently fetch a movie and update the display timestamp?
- If this system needs to scale to millions of movies across a distributed cluster, what challenges arise with keeping these ordered maps synchronized?
- How would you persist the state of these maps so that the billboard engine can recover its state after a process crash without re-indexing from scratch?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.