Order the tasks
Netflix software engineer candidates report topological sort in coding rounds, one of them as the phone screen (2 reports), and a senior candidate's version also asked when each task finishes (1 report). An Airbnb candidate reported a question about scheduling tasks in Airflow, a tool that runs pipelines of tasks that depend on each other (1 report). It starts like this:
You get a build pipeline.
tasksis a list of distinct task names.needsis a list of(task, prerequisite)pairs, and each pair meanstaskcan't start untilprerequisitehas finished. Every name in a pair is intasks. A task can be in no pair at all, and the same pair can be listed twice.Write
build_order(tasks, needs). Return a list that holds every task exactly once, each one after all of its prerequisites. When several orders work, any of them passes. For now, every pipeline can run to the end.These calls show one valid answer each:
build_order(["test", "build"], [("test", "build")])returns["build", "test"]build_order(["deploy", "test", "build"], [("deploy", "test"), ("test", "build")])returns["build", "test", "deploy"]build_order(["deploy", "lint", "test"], [("deploy", "lint"), ("deploy", "test")])returns["lint", "test", "deploy"]build_order(["docs", "build"], [])returns["docs", "build"]build_order(["test", "build"], [("test", "build"), ("test", "build")])returns["build", "test"]build_order([], [])returns[]
Two more levels follow. Level 2 puts mistakes in the pipeline, such as two tasks that need each other: build_order raises ValueError when any task can never run, and a new stuck_tasks returns every such task, sorted. Level 3 gives each task a number of minutes, with as many workers as tasks, and asks when each task finishes and when the whole pipeline is done.
You don't write it yet. The lesson first turns the pairs into a graph, then builds the loop that releases a task when its last prerequisite finishes. At the end you solve the whole problem, level by level, in an interview simulation with a 45-minute timer.
This lesson builds on the grid search: Kahn's algorithm is a breadth-first search over tasks instead of cells, with each task's count of open prerequisites in a dict. Ordering work by what it depends on comes back in the work queue, in the coding builds.