As reported: Apple Senior Systems Engineer (2025), hiring manager round: a standard topological sort for processing tasks in parallel, with a follow-up on optimizing it for multi-core execution.
There are numCourses tasks labelled 0 to numCourses − 1. prerequisites[i] = [a, b] means task b must finish before task a starts.
Return an order in which all tasks can be completed. If there are several, return any of them. If it is impossible (the dependencies contain a cycle), return an empty array.
This is LeetCode 210 — Course Schedule II.
Example 1
numCourses = 2, prerequisites = [[1,0]] -> [0,1]
Example 2
numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] -> [0,1,2,3] or [0,2,1,3]
Example 3
numCourses = 1, prerequisites = [] -> [0]
Constraints: 1 ≤ numCourses ≤ 2000; 0 ≤ prerequisites.length ≤ numCourses · (numCourses − 1); prerequisites[i] = [a, b] with a ≠ b; all pairs are distinct.
Follow-ups
- The tasks run on a multi-core machine. How do you execute them in parallel while respecting the dependencies, and how do you optimize for multiple cores?
Code runner format: one JSON value per line: numCourses, then prerequisites (LeetCode's test-case format). Implement findOrder(numCourses, prerequisites); any valid order is accepted — the harness checks it and prints "ok".