Q173FreeDesign 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.

Candidate starting point
Implementation scaffold
class DeadlockDetector;
int edges[int][$];
int status[int]; // 0 = unseen, 1 = visiting, 2 = done
function void wait_on(int from, int to);
// Implement here: wait_on.
endfunction
function void clear_wait(int from);
// Implement here: clear_wait.
endfunction
function bit dfs(int node);
// Implement here: dfs.
endfunction
function bit has_deadlock();
// Implement here: has_deadlock.
endfunction
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.
