Find and Fix a Lock Ordering Deadlock in Account Transfers

Last updated: November 29, 2026

Quick Overview

A transfer function locks the source account and then the destination account; under load, two opposite transfers sometimes hang forever. Explain why, state the conditions for deadlock, and fix the code.

Microsoft
Software Engineering Fundamentals
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


A transfer function locks the source account and then the destination account; under load, two opposite transfers sometimes hang forever. Explain why, state the conditions for deadlock, and fix the code.

Concurrency fundamentals come up often in Microsoft screens for Windows, SQL Server, and Azure storage teams, where lock hierarchies are a daily concern. The interviewer wants the Coffman conditions, a fix that breaks one of them deliberately, and awareness of tools that detect lock order violations.

What the Interviewer Expects
  • Explains the hang as a circular wait between two threads
  • Names the four Coffman conditions and which one the fix breaks
  • Imposes a global lock order using a stable key
  • Handles self-transfer and keeps the balance check inside the locks
  • Mentions alternatives: try-lock with backoff, a single coarse lock, or lock-free designs
  • Mentions detection tools and lock hierarchy checks
Key Topics to Cover
Deadlock
Lock ordering
Coffman conditions
Mutual exclusion
Thread safety
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you handle a transfer that touches three or more accounts?
  • What if account ids are not comparable or can change?
  • How would you detect lock order violations in a large C++ codebase before they deadlock in production?
  • How does a database avoid this problem for concurrent transactions?
Sharpen Your Skills on Codemia

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

Practice System Design Problems
Sample Answer
Requirements

Transfers must be atomic (no money created or lost), concurrent transfers on unrelated accounts should not block each other, and the system must never hang. Insufficient funds return False....

High-Level Design

The buggy version does with src.lock: with dst.lock:. Thread 1 transfers A to B and holds A's lock; Thread 2 transfers B to A and holds B's lock; each now waits for the other forever. Deadlock requi...


Submit Your Answer
Markdown supported