Code RoomDeadlock detection resource graph
HardPrep Room Coding #180

Deadlock detection resource graph

CodingConcurrencyAlgorithms & data structuresSenior–Staff~35 min

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.

0:00 of about 35 min
InputExpectedGot
[2,2,[[0,0],[1,1]],[[0,1],[1,0]]]truenot run yetsample