Code RoomDeadlocked transactions
EasyPrep Room Coding #4765

Deadlocked transactions

CodingConcurrencyAlgorithms & data structuresEntry–Mid~15 min

A database lock manager dumps a snapshot of who is waiting for whom. txn_ids lists every open transaction, and waits holds entries written as "t3|t7", meaning t3 is blocked on a lock that t7 holds. A transaction can be blocked on only one lock at a time, so it appears at most once on the left of an entry, every name in waits also appears in txn_ids, and the ids are distinct. A transaction waiting on nobody is running right now, and a blocked transaction resumes as soon as the transaction it waits for finishes. Return the ids of the transactions that will never resume, sorted in ascending order, or an empty list when the snapshot is healthy. Remember that a transaction queued behind a stuck one is stuck too, even when it sits outside the cycle, and that a transaction waiting on itself never resumes.

Implement
stalled_transactions(txn_ids: list[str], waits: list[str]) → list[str]
Examples
in[["t1","t2","t3"],["t1|t2","t2|t3"]]out[]
in[["t1","t2","t3"],["t1|t2","t2|t3","t3|t1"]]out["t1","t2","t3"]
in[["t1","t2","t3","t4"],["t1|t2","t2|t3","t3|t2"]]out["t1","t2","t3"]
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 15 min
InputExpectedGot
[["t1","t2","t3"],["t1|t2","t2|t3"]][]not run yetsample
[["t1","t2","t3"],["t1|t2","t2|t3","t3|t1"]]["t1","t2","t3"]not run yetsample
[["t1","t2","t3","t4"],["t1|t2","t2|t3","t3|t2"]]["t1","t2","t3"]not run yetsample