30Order the tasks
30 Order the tasks
Netflix software engineer candidates report topological sort in their coding rounds: a senior candidate got it as the phone screen (1 report), and another named a question about it as their interview (1 report). A senior candidate's version had a twist on top: working out when each task finishes, given when its prerequisites finish (1 report). An Airbnb software engineer candidate reported a question about scheduling tasks in Airflow, a tool that runs pipelines of tasks that depend on each other (1 report). Course Schedule II and Parallel Courses, both tagged Topological Sort, are on Netflix's LeetCode company tag list (2 sources). This is practice in that shape, not a leaked question or LeetCode's version.
Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/30_task_order -k level1. Earlier levels must keep passing.
The input, at every level. A build pipeline. tasks is a list of distinct task names. needs is a list of
(task, prerequisite) pairs, each meaning task can't start until prerequisite has finished. Every name in a pair
is in tasks. A task can be in no pair at all, and the same pair can be listed more than once.
tasks = ["checkout", "install", "compile", "lint", "unit_tests", "bundle", "deploy", "changelog"]
needs = [
("install", "checkout"),
("compile", "install"),
("lint", "install"),
("unit_tests", "compile"),
("bundle", "compile"),
("deploy", "unit_tests"),
("deploy", "bundle"),
("deploy", "lint"),
]
Level 1. Write build_order(tasks, needs). Return a list that holds every task exactly once, with each task
after all of its prerequisites. Most pipelines have more than one such order, and any of them passes: the tests
check the rule, not one fixed list. ["checkout", "changelog", "install", "compile", "lint", "unit_tests", "bundle", "deploy"] is one valid answer for the pipeline above, and so is the same list with changelog moved to the end.
Level 1's pipelines can always run to the end.
Level 2. Pipelines with a mistake in them. If migrate needs seed and seed needs migrate, neither can ever
start, and neither can a task that waits on either one, directly or through other tasks. A task that needs itself
can never start either.
build_orderraisesValueErrorwhen any task can never run, instead of returning an order that leaves tasks out.- Write
stuck_tasks(tasks, needs): the tasks that can never run, as a sorted list, or[]when every task can.
Level 3. Each task takes a whole number of minutes, minutes[task], which can be 0. The pipeline starts at
minute 0, there are as many workers as tasks, and a task starts the moment its last prerequisite finishes, or at
minute 0 if it has none. Write finish_times(tasks, needs, minutes), which returns a pair: a dict from each task to
the minute it finishes, and the minute the whole pipeline is done, which is 0 when there are no tasks. With
minutes = {"checkout": 1, "install": 4, "compile": 6, "lint": 2, "unit_tests": 9, "bundle": 3, "deploy": 2, "changelog": 5}
unit_tests finishes at 20, bundle at 14 and lint at 7, so deploy starts at 20 and finishes at 22, and the
pipeline is done at 22. finish_times raises ValueError when a task can never run.
Things to get right: which way each pair points, the tasks that are in no pair, a task with two prerequisites waiting for both, an order that comes out short, and a finish time read only after it's final.
Be ready to talk about what comes next: how you'd pick one order when several are valid (say, alphabetical among the tasks that are ready), which tasks set the finish time and how late each other task could start without moving it, what changes with only two workers, and how you'd rerun only what a changed task affects.