Katkat

Reason About a Movie Billboard Rotation Component

NetflixSoftware Engineer · Onsite

Last 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:

  1. PriorityMap: Maps (priority_score, movie_id) to movie_metadata.
  2. TimestampMap: Maps (last_displayed_timestamp, movie_id) to movie_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_id is a unique string.
  • priority is a float.
  • Ensure update_display operations maintain the invariants of both ordered maps.
  • Time complexity for get_next_movie and update_display should ideally be O(log N).

Example

  • Initial state: Movie A (p=10), Movie B (p=5).
  • get_next_movie() returns A (highest priority).
  • update_display('A') sets A's timestamp to T1.
  • 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?