Top 5 Deadlock Interview Questions
Browse 5 real-world technical and behavioral interview questions about Deadlock. Review scenarios, edge cases, and architectural best practices.
State the four conditions for deadlock, then show me how you would stop a real program from deadlocking.
Deadlock needs all four Coffman conditions at once: mutual exclusion, hold and wait, no preemption and circular wait. Breaking any one prevents it, and the practical choice is usually circular wait, by making every thread take locks in one global order.
One global lock makes your reservation system correct and slow. How do you split it without deadlocking?
Shrink the critical section first, then replace the global lock with lock striping, a fixed array of locks keyed by resource hash. Lock ordering by ascending stripe index rules out deadlock for multi-resource bookings, tryLock bounds waiting, and invariants the old lock covered by accident each need an owner.
The same multiprocessing code runs fine on your Linux box and breaks on a colleague's Mac. What is different?
Python multiprocessing behaves differently across platforms because fork inherits parent memory while spawn starts a fresh interpreter, re-imports the module and requires picklable callables and arguments. It also connects start methods to the point an interviewer is testing.
An operation in this code takes two locks and it deadlocks every few weeks. How would you design the locking so that cannot happen?
A deadlock is a cycle in the wait-for graph, so the design goal is a graph that cannot contain one: stop holding two locks where you can, impose one total order on acquisition everywhere you cannot, and never call unknown code while holding a lock.
Why does calling .Result or .Wait() on an async method deadlock, and how do you fix it properly?
Blocking on async code with .Result or .Wait can deadlock when the awaited continuation needs the same SynchronizationContext that is blocked. In ASP.NET Core the common failure is thread-pool starvation; the fix is async all the way up.