Task Order with Dependencies
Updated
Problem statement
Given n tasks labeled from 0 to n - 1 and an array dependencies, return the order in which the tasks are done, or an empty array if the dependencies form a cycle.
Each dependencies[i] = [a, b] means task a must be finished before task b can start. Tasks are done one at a time. A task is ready when every task it depends on is finished. Whenever more than one task is ready, you always do the ready task with the smallest label next.
In a cycle, tasks wait on each other, so none of them can ever become ready.
Function
findTaskOrder(n: int, dependencies: int[][]) → int[]Examples
Example 1
n = 5dependencies = [[3,1],[0,1],[1,2],[4,2]]return = [0,3,1,4,2]Tasks 0, 3 and 4 depend on nothing, so they are ready at the start, and task 0 goes first. Task 1 still waits for task 3, so only tasks 3 and 4 are ready, and task 3 has the smaller label. Now tasks 1 and 4 are both ready, and task 1 has the smaller label, so it goes before task 4. Task 2 becomes ready only after tasks 1 and 4 are both done.
Example 2
n = 3dependencies = [[0,1],[1,2],[2,1]]return = []Task 0 can be done, but task 1 waits for task 2 and task 2 waits for task 1. Neither can ever start, so the answer is an empty array.
Example 3
n = 3dependencies = []return = [0,1,2]No task depends on another, so all three are ready at once and are done from the smallest label up.
Constraints
1 <= n <= 1050 <= dependencies.length <= 105dependencies[i].length == 20 <= a, b < na != b- All pairs [a, b] are distinct.
More Notion interview questions
- Nested Todo ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top Error Types in a Time WindowONSITE INTERVIEW · Seen Jun 2026
- Text Document HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- DAG Downstream NodesONSITE INTERVIEW · Seen Apr 2026
- Mutable Table AggregationsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
- Page Permission InheritancePHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
- Sliding Window MedianONSITE INTERVIEW · Seen Oct 2025
- Task Row ModificationsONSITE INTERVIEW · Seen Oct 2025
- JSON Block Tree OperationsONSITE INTERVIEW · Seen Jul 2025
- Block Type with the Most ChildrenPHONE SCREEN · Seen Mar 2026
Similar questions at other companies
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Lexicographically Smallest Course OrderLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- ETL Pipeline SchedulerUnity · ONSITE INTERVIEW
- Microservice Deployment OrderGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Course Schedule IIWalmart · ONSITE INTERVIEWWalmart interview questions
- Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions