Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Operating Systems
Operating Systemsmedium

What causes deadlocks and how can you prevent them?

Tags
#deadlock#locks#concurrency
Back to categoryPractice quiz

Answer

Deadlocks require four conditions: mutual exclusion, hold-and-wait, no preemption, and circular wait. Prevention breaks at least one condition (e.g., ordering locks or using timeouts).

Advanced answer

Deep dive

Strategies:

  • Lock ordering to prevent cycles.
  • Try-lock + backoff to avoid hold-and-wait.
  • Timeouts and deadlock detection for recovery.
  • Reduce shared resources to minimize contention.

Examples

Lock ordering rule:

Always acquire locks in order: A -> B -> C

Common pitfalls

  • Inconsistent lock ordering across code paths.
  • Long critical sections increasing contention.
  • Ignoring deadlocks in rare error paths.

Interview follow-ups

  • When would you prefer detection vs prevention?
  • How do you debug a deadlock in production?
  • What is livelock and how is it different?

Related questions

Operating Systems
Processes vs threads — what’s the difference and when does it matter?
#processes#threads#concurrency
Testing
How do you test asynchronous or concurrent code?
#async#concurrency#determinism
PostgreSQL
Advisory locks: what are they and when would you use them?
#postgres#locks
#advisory
PostgreSQL
MVCC in Postgres: why don’t readers block writers?
#postgres#mvcc#concurrency
Databases
Deadlock: what is it and how do databases resolve it?
#database#transactions#locks