Task Dependency Ordering
Updated
Problem statement
You are given two parallel arrays that serialize a task-to-prerequisites map:
tasks[i]is a unique task identifier.prerequisites[i]contains the direct prerequisites oftasks[i].
Return a topological order containing every task exactly once, with every prerequisite before the task that depends on it.
Use a stable first-in, first-out ready queue. Add initially ready tasks in their order in tasks. After processing a task, examine its dependents in tasks order and append each dependent when its final prerequisite has been processed.
If the dependency graph contains a cycle, return an empty array. Therefore, the workflow can be completed exactly when the returned array has the same length as tasks.
A task map may mention a prerequisite only as a value, without giving it a map entry. Its graph still includes that task, with no prerequisites of its own. Before using this array interface, include every such name in tasks and give it an empty prerequisite row. The first example already includes Task0 in that form. The function receives this expanded representation and leaves both input arrays unchanged.
Function
findTaskOrder(tasks: String[], prerequisites: String[][]) → String[]Examples
Example 1
tasks = ["Task0","Task2","Task1","Task3"]prerequisites = [[],["Task0"],["Task0"],["Task0","Task1","Task2"]]return = ["Task0","Task2","Task1","Task3"]Task0 is initially ready. Processing it makes Task2 and Task1 ready in input order. Task3 becomes ready only after both have been processed.
The order ["Task0","Task1","Task3","Task2"] is invalid because it places Task3 before its prerequisite Task2.
Example 2
tasks = ["A","B","C"]prerequisites = [["C"],["A"],["B"]]return = []The dependencies form the cycle A -> B -> C -> A, so no complete topological order exists.
Constraints
1 <= tasks.length <= 10^5.prerequisites.length == tasks.length.- Every task identifier is non-empty and appears exactly once in
tasks. - Every prerequisite names a task in
tasks. - Each prerequisite appears at most once for a given task.
- The total number of prerequisite entries is at most
2 * 10^5.
More Citadel Phone Screen questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Order Book with Cancel by Order IDPHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Recipes You Can PrepareIndeed · PHONE SCREENIndeed interview questions
- Distances to CyclesBelvedere Trading · OA
- Order DAG Nodes from Leaves to RootsVercel · ONSITE INTERVIEW
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions