Thread-Safe Topological Sort Framework

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-30

Problem Statement

Design and implement a thread-safe graph processing framework that supports topological sorting of a Directed Acyclic Graph (DAG).


Design and implement a thread-safe, low-level topological sort framework that supports concurrent graph building, parallel execution of tasks in valid topological order, cycle detection, error handling, and controlled shutdown.


The system should be extensible enough to allow parallel execution of nodes (tasks) once their dependencies are resolved.


🔶 Core Requirements

1.<span></span>Graph Representation
2.<span></span>Implement a graph data structure supporting:
3.<span></span>Directed edges.
4.<span></span>Efficient in-degree and adjacency tracking.
5.<span></span>Dynamic addition of nodes and edges (before execution starts).
6.<span></span>Topological Sort
7.<span></span>Implement topological sorting using Kahn’s Algorithm or DFS-based approach.
8.<span></span>Must detect cycles and throw an exception if the graph is not a DAG.
9.<span></span>Concurrency / Thread Safety
10.<span></span>Multiple threads may add nodes and edges concurrently before execution starts.
11.<span></span>The topological sort algorithm itself should support parallel execution of independent nodes.
12.<span></span>Ensure atomic updates when:
13.<span></span>Decreasing in-degree counts.
14.<span></span>Adding ready nodes to the execution queue.
15.<span></span>Execution Model
16.<span></span>Each node can be associated with a task (Runnable/Callable).
17.<span></span>Once all dependencies of a node are satisfied, the node’s task should be executed.
18.<span></span>Independent nodes should run in parallel across worker threads.
19.<span></span>Error Handling
20.<span></span>If a task throws an exception, it should be logged but must not crash the scheduler.
21.<span></span>Support an option to either:
22.<span></span>Continue with remaining tasks, or
23.<span></span>Halt execution.
24.<span></span>Termination
25.<span></span>Provide a block_until_finish() method that waits until all tasks are processed.
26.<span></span>Provide a shutdown() method to stop accepting new nodes/edges.


🔶 Constraints

1.<span></span>Graph size: up to 10^5 nodes and 10^6 edges.
2.<span></span>Concurrency: multiple producers adding edges/nodes, multiple worker threads executing tasks.
3.<span></span>Execution must maintain valid topological order.


🔶 Example Use Case

1.<span></span>Build System (like make)
2.<span></span>Files (nodes) with compilation dependencies (edges).
3.<span></span>Independent files can compile in parallel once dependencies are resolved.
4.<span></span>Workflow Engine
5.<span></span>Data processing jobs with dependency chains.
6.<span></span>Independent jobs should execute concurrently.
7.<span></span>Task Scheduler
8.<span></span>Tasks with precedence constraints (e.g., ETL pipeline).


🔶 Expectations

1.<span></span>Implement a class ThreadSafeTopologicalExecutor with APIs:
<div><div><br /></div><div>void addNode(String id, Runnable task);</div><div>void addEdge(String from, String to);</div><div>void execute(); // starts processing in topological order</div><div>void block_until_finish(); // waits for completion</div><div>void shutdown();</div></div>
1.<span></span>Ensure:
2.<span></span>Thread safety in addNode() and addEdge().
3.<span></span>Use of ConcurrentQueue / synchronized blocks / locks where needed.
4.<span></span>Efficient scheduling via a thread pool.
5.<span></span>Unit tests covering:
6.<span></span>Simple DAG.
7.<span></span>Parallel execution of independent tasks.
8.<span></span>Cycle detection.
9.<span></span>Stress test with concurrent insertions.


Solution

Comments