Compute Minimum Task Completion Time
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph algorithms (topological sort and DFS), dependency scheduling, cycle detection, and critical-path computation for tasks with durations.
Part 1: Minimum Task Completion Time with BFS Topological Sort
Constraints
- 0 <= n <= 100000
- len(durations) == n
- 0 <= durations[i] <= 10^9
- 0 <= len(dependencies) <= 200000
- Each dependency is a pair [a, b] with 0 <= a, b < n
Examples
Input: (3, [3, 2, 5], [[0, 2], [1, 2]])
Expected Output: 8
Explanation: Tasks 0 and 1 run in parallel. Task 2 starts after both are done, so it starts at time 3 and finishes at time 8.
Input: (5, [1, 2, 3, 4, 2], [[0, 2], [1, 2], [2, 3]])
Expected Output: 9
Explanation: Task 2 must wait for tasks 0 and 1, so it finishes at time 5. Then task 3 finishes at time 9. Task 4 is independent and finishes earlier.
Input: (3, [1, 1, 1], [[0, 1], [1, 2], [2, 0]])
Expected Output: -1
Explanation: The dependencies form a cycle, so no valid ordering exists.
Input: (0, [], [])
Expected Output: 0
Explanation: There are no tasks, so the total completion time is 0.
Hints
- Use indegree counts to find which tasks can start immediately.
- As you process tasks in topological order, track the earliest finish time for each task based on the maximum finish time among its prerequisites.
Part 2: Minimum Task Completion Time with DFS Memoization
Constraints
- 0 <= n <= 100000
- len(durations) == n
- 0 <= durations[i] <= 10^9
- 0 <= len(dependencies) <= 200000
- Each dependency is a pair [a, b] with 0 <= a, b < n
Examples
Input: (4, [2, 1, 4, 3], [[0, 2], [1, 2], [2, 3]])
Expected Output: 9
Explanation: Tasks 0 and 1 can run first. Task 2 must wait for both, so it finishes at time 6. Task 3 then finishes at time 9.
Input: (4, [5, 2, 1, 4], [])
Expected Output: 5
Explanation: With no dependencies, all tasks run in parallel, so the answer is the maximum single duration.
Input: (2, [3, 4], [[0, 1], [1, 0]])
Expected Output: -1
Explanation: Tasks 0 and 1 depend on each other, forming a cycle.
Input: (0, [], [])
Expected Output: 0
Explanation: No tasks means no time is needed.
Hints
- Think of each task's completion time as its own duration plus the maximum completion time among all of its prerequisites.
- Use a 3-state DFS marking scheme: unvisited, visiting, and fully processed. Re-entering a visiting node means there is a cycle.