Detect a Potential Deadlock From Lock Acquisition Traces

Last updated: November 29, 2026

Quick Overview

You are given traces from many threads, each a sequence of events like ('acquire', lock) and ('release', lock). Return True if some pair or chain of threads acquires locks in conflicting orders, meaning the lock-order graph built from these traces contains a cycle.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


You are given traces from many threads, each a sequence of events like ('acquire', lock) and ('release', lock). Return True if some pair or chain of threads acquires locks in conflicting orders, meaning the lock-order graph built from these traces contains a cycle.

A Meta onsite coding question with a systems flavor, in the spirit of the lock-order checkers used on large C++ services. The interviewer checks that you build edges only from locks currently held to the newly acquired one, then run a standard cycle check, and that you understand this finds potential deadlocks, not only ones that happened.

What the Interviewer Expects
  • Track the set of locks held at each point in each trace
  • Add an edge held -> acquired for every held lock
  • Run a cycle check over the combined graph
  • Explain why this flags potential rather than observed deadlocks
  • Discuss false positives such as gate locks
Key Topics to Cover
cycle detection
lock ordering
directed graph
Kahn's algorithm
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you report the lock cycle and the threads that produced each edge?
  • How would you suppress false positives where a common outer lock serializes both orders?
  • How would you do this online inside a running service with low overhead?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: traces, a list of event lists. Each event is (op, lock) with op in {'acquire', 'release'}.
  • Output: True if the lock-order graph has a cycle.
  • Locks are not reentrant; releases ma...
High-Level Design

Deadlock needs a cycle of waits: thread 1 holds A and wants B while thread 2 holds B and wants A. Lock-order analysis captures this without needing the bad interleaving to actually happen:

  1. Replay ...

Submit Your Answer
Markdown supported