Software Engineer Interview Question

What causes a deadlock, and how would you prevent one?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

A deadlock needs four conditions at once: mutual exclusion, hold and wait, no preemption, and a circular wait. Break any one and it cannot happen. In practice you break the circular wait by always acquiring locks in the same global order, and you add lock timeouts so a stuck transaction fails fast rather than hanging. Keeping transactions short and touching rows in a consistent order gets you most of the way there.

Why interviewers ask this

This tests whether you know the theory and can turn it into a rule a team can actually follow. Interviewers want the circular wait insight and the ordering fix, plus recognition that deadlocks are common in databases and not only in threaded code. Mentioning lock timeouts, retry with backoff, and the fact that the database will pick a victim and abort it signals production experience.

How to structure your answer

  • List the four conditions that must all hold.
  • Pick the one you can break in practice: circular wait.
  • Give the consistent lock ordering rule concretely.
  • Add timeouts and retries as the safety net.

Example answer

Spoken example, first person

Deadlock needs four things together, and the one I can actually control is the circular wait. So the rule I put in place is that every code path takes locks in the same order. If a transfer touches two accounts, I sort by account id and lock the lower one first, and then it does not matter which direction the money is going, no cycle can form. The second lever is time. I keep transactions short, so I never hold a row lock across a network call to a payment provider, which is a classic way to turn a hundred millisecond transaction into a five second one. Then I set a lock timeout and retry with jitter, because sooner or later the database will detect a cycle, pick a victim and abort it, and I would rather that path be handled than surfaced as a 500. In Postgres the deadlock log entries name both statements involved, and both times I have used them the fix was ordering.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How does the database detect a deadlock?
  • What is livelock and how is it different?
  • Would optimistic locking avoid this problem entirely?

Related software engineer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot