Implement a DAG task graph
Company: Notion
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates competency with directed acyclic graphs, graph algorithms (topological sorting), dependency modeling, and implementing data structures to represent task graphs.
Constraints
- 0 <= number of operations <= 10^4
- There are at most 10^4 distinct tasks
- Each task ID is a lowercase string of length 1 to 20
- Any `add_dependency` operation in the input keeps the graph acyclic
Examples
Input: [("add_task", "build"), ("add_task", "test"), ("add_task", "deploy"), ("add_dependency", "build", "test"), ("add_dependency", "test", "deploy"), ("get_execution_order",)]
Expected Output: [['build', 'test', 'deploy']]
Explanation: This is a simple chain: build must happen before test, and test before deploy.
Input: [("add_task", "b"), ("add_task", "a"), ("add_task", "c"), ("add_dependency", "a", "c"), ("get_execution_order",)]
Expected Output: [['a', 'b', 'c']]
Explanation: Tasks `a` and `b` are both initially ready. Lexicographic tie-breaking chooses `a` first, then `b`, then `c`.
Input: [("add_task", "a"), ("add_task", "a"), ("add_task", "b"), ("add_dependency", "a", "b"), ("add_dependency", "a", "b"), ("get_execution_order",), ("add_task", "c"), ("get_execution_order",)]
Expected Output: [['a', 'b'], ['a', 'b', 'c']]
Explanation: Duplicate task and duplicate edge do nothing. After adding isolated task `c`, it is included in the next execution order.
Input: [("get_execution_order",)]
Expected Output: [[]]
Explanation: An empty graph has an empty valid execution order.
Input: []
Expected Output: []
Explanation: If there are no operations, there are no execution-order queries to answer.
Hints
- A topological ordering can be built using Kahn's algorithm by tracking the indegree of each task.
- Use a min-heap (priority queue) for currently available tasks so the returned order is deterministic and lexicographically smallest.