Compute Earliest Completion Times
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates understanding of directed acyclic graphs, dependency resolution, computation of earliest task completion times, and cycle detection within prerequisite relationships.
Constraints
- `0 <= n <= 200000`
- `len(duration) == n`
- `1 <= duration[i] <= 10^9` for each valid `i`
- `0 <= len(prerequisites) <= 200000`
- Each prerequisite is a pair `[a, b]` with `0 <= a, b < n`
Examples
Input: (4, [3, 2, 4, 1], [[0, 2], [1, 2], [2, 3]])
Expected Output: ([3, 2, 7, 8], 8)
Explanation: Tasks 0 and 1 can run immediately, finishing at 3 and 2. Task 2 starts after both are done, so it starts at 3 and finishes at 7. Task 3 finishes at 8.
Input: (3, [5, 1, 2], [])
Expected Output: ([5, 1, 2], 5)
Explanation: With no prerequisites, all tasks run in parallel. Each task finishes after its own duration, and the total time is the maximum of those values.
Input: (0, [], [])
Expected Output: ([], 0)
Explanation: There are no tasks, so the earliest finish list is empty and the total time is 0.
Input: (6, [2, 4, 3, 1, 5, 2], [[0, 3], [1, 3], [1, 4], [2, 4], [3, 5], [4, 5]])
Expected Output: ([2, 4, 3, 5, 9, 11], 11)
Explanation: Tasks 0, 1, and 2 finish at 2, 4, and 3. Task 3 starts at 4 and finishes at 5. Task 4 starts at 4 and finishes at 9. Task 5 starts at 9 and finishes at 11.
Input: (3, [1, 2, 3], [[0, 1], [1, 2], [2, 0]])
Expected Output: None
Explanation: The prerequisites form a cycle, so no valid schedule exists.
Hints
- A valid schedule exists only if the prerequisite graph is a DAG. Think about using topological sorting to process tasks in dependency order.
- For each task, track the maximum completion time among all of its prerequisites. Its own earliest finish is that value plus its duration.