Schedule Dependent Tasks So Each Runs After Its Prerequisites
Company: Apple
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
A job scheduler receives a list of task names and a list of dependencies between them. A dependency `[before, after]` means task `before` must finish before task `after` can start. Tasks run one at a time. Return the order in which to run all the tasks so that every task starts only after all of its prerequisites have finished.
When several tasks are ready at the same time, run the one with the smallest name. If the dependencies contain a cycle, so that some tasks can never start, return an empty list.
### Function Signature
```python
def schedule_tasks(tasks: list[str], dependencies: list[list[str]]) -> list[str]:
```
### Rules
- A task is ready when every task it depends on has already been placed in the order. A task with no prerequisites is ready from the start.
- Whenever more than one task is ready, the next task is the smallest ready name under Python string comparison.
- When scheduling succeeds, every name in `tasks` appears in the result exactly once.
- A dependency pair may appear more than once; repeats mean the same as a single copy.
- If tasks remain but none is ready, return `[]`.
### Constraints
- `1 <= len(tasks) <= 10^4`
- `0 <= len(dependencies) <= 5 * 10^4`
- Task names are unique, 1 to 20 characters long, and contain only lowercase English letters, digits and `_`.
- Each dependency is a list of two different names, both in `tasks`.
### Examples
#### Example 1
```text
Input: tasks = ["build","deploy","test","lint"], dependencies = [["build","test"],["test","deploy"],["lint","deploy"]]
Output: ["build","lint","test","deploy"]
```
`build` and `lint` have no prerequisites, and `build` is the smaller name. Once `build` is placed, `lint` and `test` are both ready and `lint` is smaller. `deploy` waits for both `test` and `lint`.
#### Example 2
```text
Input: tasks = ["a","b","c"], dependencies = [["a","b"],["b","c"],["c","a"]]
Output: []
```
Each task waits for another task in the cycle, so none can start.
#### Example 3
```text
Input: tasks = ["c","a","b"], dependencies = []
Output: ["a","b","c"]
```
With no dependencies every task is ready at once, so the tasks run in name order.
Overview: A practical coding problem about scheduling dependent tasks: given task names and prerequisite pairs, return a run order in which every task follows its prerequisites, breaking ties by name, or an empty list when a cycle makes scheduling impossible. It tests dependency graph processing and cycle detection.
Read the full Apple Machine Learning Engineer interview experience this question came from
A job scheduler receives a list of task names `tasks` and a list of dependencies `dependencies` between them. A dependency `[before, after]` means task `before` must finish before task `after` can start. Tasks run one at a time. Return the order in which to run all the tasks so that every task starts only after all of its prerequisites have finished.
When several tasks are ready at the same time, run the one with the smallest name. If the dependencies contain a cycle, so that some tasks can never start, return an empty list.
### Rules
- A task is ready when every task it depends on has already been placed in the order. A task with no prerequisites is ready from the start.
- Whenever more than one task is ready, the next task is the smallest ready name under Python string comparison. For these names that means comparing character codes from left to right: digits come before `_`, and `_` comes before lowercase letters, so `"10"` comes before `"9"` and `"_x"` comes before `"a"`. When one name is a prefix of another, the shorter name comes first (`"a"` before `"ab"`).
- When scheduling succeeds, every name in `tasks` appears in the result exactly once.
- A dependency pair may appear more than once; repeats mean the same as a single copy.
- If tasks remain but none is ready, return `[]`.
### Constraints
- `1 <= len(tasks) <= 10^4`
- `0 <= len(dependencies) <= 5 * 10^4`
- Task names are unique, 1 to 20 characters long, and contain only lowercase English letters, digits and `_`.
- Each dependency is a list of two different names, both in `tasks`.
No count in this problem exceeds 2^31-1, so 32-bit integers are enough in every language.
### Input and output by language
- Python and JavaScript receive `tasks` as a list of strings and `dependencies` as a list of two-element lists, and return a list of strings.
- Java receives `java.util.List<String> tasks` and `java.util.List<java.util.List<String>> dependencies`, and returns a `java.util.List<String>`.
- C++ receives `const std::vector<std::string>& tasks` and `const std::vector<std::vector<std::string>>& dependencies`, and returns a `std::vector<std::string>`.
Names use only ASCII characters, so the default string ordering in JavaScript, Java and C++ matches Python string comparison.
### Examples
#### Example 1
```text
Input: tasks = ["build","deploy","test","lint"], dependencies = [["build","test"],["test","deploy"],["lint","deploy"]]
Output: ["build","lint","test","deploy"]
```
`build` and `lint` have no prerequisites, and `build` is the smaller name. Once `build` is placed, `lint` and `test` are both ready and `lint` is smaller. `deploy` waits for both `test` and `lint`.
#### Example 2
```text
Input: tasks = ["a","b","c"], dependencies = [["a","b"],["b","c"],["c","a"]]
Output: []
```
Each task waits for another task in the cycle, so none can start.
Constraints
- 1 <= len(tasks) <= 10^4
- 0 <= len(dependencies) <= 5 * 10^4
- Task names are unique, 1 to 20 characters long, and contain only lowercase English letters, digits and `_`.
- Each dependency is a list of two different names, both in `tasks`.
Examples
Input: (['build', 'deploy', 'test', 'lint'], [['build', 'test'], ['test', 'deploy'], ['lint', 'deploy']])
Expected Output: ['build', 'lint', 'test', 'deploy']
Explanation: Example 1: build and lint start ready and build is smaller, then lint beats the newly ready test, and deploy waits for both test and lint.
Input: (['a', 'b', 'c'], [['a', 'b'], ['b', 'c'], ['c', 'a']])
Expected Output: []
Explanation: Example 2: the three tasks form one full cycle, so none can start.
Hints
- After each task is placed, look again at everything that is ready: a task that just became ready can have a smaller name than tasks that were waiting before it.
- A repeated dependency pair must not make a task wait any longer than a single copy would.
- If tasks remain but none of them is ready, the rules ask for an empty list, even when some tasks were already placed.