Q115FreeDesign Verification
Detect a deadlock in a wait graph
Interview prompt
Question
Maintain runtime wait dependencies among agents or queues. A directed edge from A to B means A is waiting on B. Return whether any dependency cycle exists.

Starting point
Question code
class DeadlockDetector;
function void wait_on(int from, int to);
function void clear_wait(int from);
function bit has_deadlock();
endclassReviewed example
Trace one case
Input
add waits 1->2 and 2->3; query; add 3->1; queryExpected output
no deadlock, then deadlock detectedThe first graph is an acyclic chain; the final edge closes the directed cycle 1->2->3->1.
What to cover
Requirements
- Add a directed dependency and create target-only nodes as needed.
- Clear every outgoing dependency for one agent.
- Detect a cycle anywhere in a disconnected graph.
- Do not report a converging acyclic dependency chain as a deadlock.

