Netflix questions

Task Order

NetflixOnsiteMedium

TaskGraph(n) manages n tasks labeled 0 to n - 1. add_dependency(before, after) states that task before must finish before task after can start. The same pair can be added more than once, and a task can depend on itself.

Part 1: Smallest valid order

order() returns an order of all n tasks in which every task appears after all of its prerequisites. When several orders exist, it returns the lexicographically smallest one, comparing the lists element by element. When the dependencies contain a cycle, so that no order exists, it returns [].

  1. Example 1
    init
    [5]
    operations
    [["order",[]],["add_dependency",[3,1]],["add_dependency",[4,1]],["add_dependency",[1,0]],["order",[]],["add_dependency",[2,0]],["order",[]]]
    Output
    [[0,1,2,3,4],null,null,null,[2,3,4,1,0],null,[2,3,4,1,0]]

    Why: With no dependencies the order is 0, 1, 2, 3, 4. After adding 3 -> 1, 4 -> 1 and 1 -> 0, tasks 2, 3 and 4 can go first, and 1 and 0 must wait: the smallest valid order is 2, 3, 4, 1, 0. Adding 2 -> 0 changes nothing.

Constraints

  • 1 <= n <= 10^4
  • 0 <= before, after < n
  • 1 <= len(operations) <= 10^4
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question