Interview replay

Full round replay — deadlock

10 minintermediate115 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

What causes a deadlock and how do you prevent it?

Budget: 45 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “What conditions have to hold for one?

    Testing: Whether they know which condition is actually attackable.

    Scoring

    Pass: Mutual exclusion, hold-and-wait, no pre-emption and circular wait, all four at once. Breaking any one prevents it, and circular wait is the only one you can break without changing what the code does.

    Fail: Recites all four with no view on which is practical.

  2. 2. “Show me the fix.

    Testing: Can they write it, not just name it?

    Scoring

    Pass: Sort the two locks by a stable, total key — account id, primary key, path — and take the lower one first in every thread. Three lines, no timeouts, no retries, no coordination between threads.

    Fail: Says 'avoid nested locks' or 'use a timeout' without the ordering idea.

  3. 3. “Would you order by identityHashCode?

    Testing: A specific trap from Java Concurrency in Practice.

    Scoring

    Pass: No — it is not guaranteed unique, so two objects can collide and both threads fall back to their own order. If identity is genuinely all you have, use a third tie-breaker lock for the collision case. A business key is better.

    Fail: Says yes, or has not considered collisions.

  4. 4. “What if you cannot impose an order?

    Testing: The fallback, and its cost.

    Scoring

    Pass: tryLock with a timeout, releasing everything already held on the way out, then retrying with backoff and jitter. It converts a hang into a failure you now have to handle — worse than ordering, better than stopping.

    Fail: Offers tryLock without mentioning releasing the first lock or handling the failure.

  5. 5. “Your tryLock returns false. What do you do?

    Testing: The half people skip.

    Scoring

    Pass: Handle it explicitly — throw, retry with backoff, queue, or return busy. Silently skipping the work is the worst option: under load the system quietly stops doing things and the only symptom is a number that stops adding up.

    Fail: Leaves the else branch empty.

  6. 6. “How do you detect one in production?

    Testing: Have they actually done it?

    Scoring

    Pass: jstack or jcmd Thread.print — it prints 'Found one Java-level deadlock' and names both threads and locks. From inside, ThreadMXBean.findDeadlockedThreads(). Take the dump before restarting, because the restart destroys the evidence.

    Fail: Says they would add logging, or restart first.

  7. 7. “What does it look like in your dashboards?

    Testing: Whether they would recognise one at 3am.

    Scoring

    Pass: Flat CPU, flat heap, and work stopping. A deadlock burns no CPU, so CPU alarms miss it entirely — alert on task completion rate and latency instead.

    Fail: Expects high CPU.

  8. 8. “Can you interrupt a deadlocked thread?

    Testing: The fact that decides synchronized versus ReentrantLock.

    Scoring

    Pass: Not one blocked entering a synchronized block. The interrupt flag gets set and the thread stays BLOCKED, because monitor entry is not an interruptible operation. Nothing external frees it — the process restarts. ReentrantLock.lockInterruptibly can be interrupted.

    Fail: Thinks interrupt works, or suggests Thread.stop.

  9. 9. “So should everything use ReentrantLock?

    Testing: Whether they weigh the cost.

    Scoring

    Pass: No. synchronized is harder to get wrong because it releases automatically, and the JIT handles it well. Reach for ReentrantLock when you need a timeout, cancellation, several conditions, or acquire and release in different scopes.

    Fail: Absolute answer either way.

  10. 10. “A dump shows twenty threads blocked on one lock and no cycle. Is that a deadlock?

    Testing: The failure that looks the same and is not.

    Scoring

    Pass: No — one thread is holding the lock across something slow, probably a remote call. The detector reports nothing and the service is just as stalled. The fix is to do the I/O outside the lock, not to reorder anything.

    Fail: Calls it a deadlock, or has no second hypothesis.

  11. 11. “How is a database deadlock different?

    Testing: Breadth.

    Scoring

    Pass: Same cycle, different ending — the database detects it and kills one transaction, so you get a retryable exception rather than a hang. Prevention is identical: touch rows in a consistent order, keep transactions short, ORDER BY id on bulk updates.

    Fail: Assumes the database hangs too.

Score yourself

11/11 — you can diagnose one at 3am and prevent the next 8-10 — solid; the interrupt and no-cycle answers are where points go 5-7 — you know the definition but not the diagnosis; redo the Challenge 0-4 — deadlock two threads yourself and read the dump

← Back to What causes a deadlock and how do you prevent it?