Code Room
CodingHardcod-g1020
Subject Deadlock detection resource allocationLevel Senior–Staff~35 minCommon in Concurrency · Algorithms & data structures interviewsIndustries Software development, Technology

Question

Detect deadlock from a resource-allocation graph with single-instance resources. There are p processes (0..p-1) and r resources (0..r-1). `held` is a list of [resource, process] meaning that resource is currently allocated to that process. `wanted` is a list of [process, resource] meaning that process is blocked waiting to acquire that resource. With single-instance resources, deadlock exists iff the resource-allocation graph has a cycle. Build the wait-for graph (process A waits-for process B if A wants a resource that B holds) and return True if a cycle (deadlock) exists, else False. Assume each resource is held by at most one process.

Implement
rag_deadlock(p: int, r: int, held: list[list[int]], wanted: list[list[int]]) → bool
Examples
in[2,2,[[0,0],[1,1]],[[0,1],[1,0]]]outtrue
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.