Problem · Design

Movie Billboard Rotation

Learn this problem
MediumNetflix logoNetflixFULLTIMEONSITE INTERVIEW

Problem statement

Process an ordered sequence of operations for a movie billboard. The billboard keeps an active rotation of movie IDs.

  • ["ADD", movieId]: if movieId is inactive, append it to the tail of the rotation and assign it a new monotonically increasing position. Adding an active ID is a no-op.
  • ["REMOVE", movieId]: remove the active movie. Removing a missing ID is a no-op. A later successful re-add receives a new tail position; old positions are never reused.
  • ["NEXT"]: return the active successor after the last displayed position. The first non-empty call returns the active head, and traversal wraps to the head after the tail. If no movie is active, return NONE without moving the cursor.

Only NEXT contributes an element to the returned array, in operation order.

Do not use a heap. Model the active rotation with an ordered position-to-movie map and a movie-to-position map. Multithreading, distributed scaling, and durable storage are follow-up discussion topics and are outside the judged single-threaded core.

Function

rotateBillboard(operations: String[][]) → String[]

Examples

Example 1

operations = [["ADD","A"],["ADD","B"],["ADD","C"],["NEXT"],["NEXT"],["REMOVE","B"],["NEXT"],["ADD","B"],["NEXT"],["NEXT"]]return = ["A","B","C","B","A"]

The first two calls display A and B. Removing B leaves the cursor at its old position, so the next successor is C. Re-adding B places it after C, then the rotation wraps to A.

Example 2

operations = [["NEXT"],["ADD","x"],["ADD","x"],["NEXT"],["REMOVE","missing"],["NEXT"],["REMOVE","x"],["NEXT"],["ADD","x"],["NEXT"]]return = ["NONE","x","x","NONE","x"]

An empty rotation returns NONE. Duplicate ADD and missing REMOVE operations do nothing. After removal, re-adding x creates a new active tail entry.

Example 3

operations = [["ADD","a"],["ADD","b"],["ADD","c"],["ADD","d"],["NEXT"],["REMOVE","b"],["REMOVE","c"],["NEXT"],["REMOVE","d"],["NEXT"],["ADD","c"],["NEXT"]]return = ["a","d","a","c"]

Deleted positions are skipped. After d is removed, the next active position wraps to a. Re-added c receives a new tail position and is the next successor.

Constraints

  • 1 <= operations.length <= 10^5.
  • Each operation is exactly ADD, REMOVE, or NEXT with the stated arity.
  • Each movie ID contains between 1 and 64 printable ASCII characters and is not NONE.
  • The operation sequence is evaluated by one thread.

More Netflix problems

drafts saved locally
public String[] rotateBillboard(String[][] operations) {
  // write your code here
}
operations[["ADD","A"],["ADD","B"],["ADD","C"],["NEXT"],["NEXT"],["REMOVE","B"],["NEXT"],["ADD","B"],["NEXT"],["NEXT"]]
expected["A", "B", "C", "B", "A"]
checking account