Netflix questions

Looping Movie Playlist

NetflixOnsiteMedium

A Playlist is built from a list of [movie_id, score] pairs with distinct ids. movie_id is a non-empty string of lowercase letters and score is an integer. A higher score plays first; among equal scores the smaller id (by string comparison) plays first.

A loop is one pass in which every movie currently in the playlist is served exactly once. A loop ends only when next() finds that every movie has already been served in it; that same call starts a new loop in which every movie is unserved again.

Part 1: Serve in score order

Implement next(). It returns the id of the unserved movie with the highest score, or the smallest id among ties, and marks it served in the current loop. If every movie is already served, that call first starts a new loop where every movie is unserved, then serves from it as usual. If the playlist has no movies, return None.

  1. Example 1
    init
    [[["a",3],["b",5],["c",5],["d",1],["e",3]]]
    operations
    [["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]],["next",[]]]
    Output
    ["b","c","a","e","d","b","c","a","e","d","b"]

    Why: b and c tie at score 5, so the smaller id b goes first; then a and e tie at 3, so a goes first. After d every movie is served, so the sixth call starts a new loop and serves b again.

Constraints

  • 0 <= len(movies) <= 10^4
  • 1 <= len(operations) <= 10^4
  • -10^9 <= score <= 10^9
  • 1 <= len(movie_id) <= 10
  • movie_id consists of lowercase letters
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Looping Movie Playlist: Netflix interview question | LoopCoach