Fix the Deadlock in Concurrent Credit Transfers Between Organizations

Last updated: November 29, 2026

Quick Overview

A service moves prepaid API credits between organization balances, and under load two transfers in opposite directions hang forever. Explain the deadlock and implement a transfer function that is safe under concurrency and never creates or destroys credits.

OpenAI
Software Engineering Fundamentals
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Phone Screen
Software Engineering Fundamentals
Easy

0

0

0 solved


A service moves prepaid API credits between organization balances, and under load two transfers in opposite directions hang forever. Explain the deadlock and implement a transfer function that is safe under concurrency and never creates or destroys credits.

OpenAI bills API usage against organization credit balances, and internal tools move credits between orgs and projects. This is a warm-up fundamentals question: the interviewer wants the four deadlock conditions, a concrete fix, and an invariant you can test.

What the Interviewer Expects
  • Identifies the circular wait created by locking source then destination
  • Fixes it with a global lock order such as sorting by account id
  • Checks the balance only while holding both locks
  • Handles self-transfer and insufficient funds
  • Proposes a stress test that checks the total is conserved
Key Topics to Cover
deadlock
lock ordering
mutual exclusion
invariants
stress testing
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 do this against a SQL database instead of in-memory locks?
  • What if a transfer touches three or more accounts?
  • When would you prefer a try-lock with backoff over lock ordering?
  • How would you detect a deadlock in production after the fact?
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

Transfer amount from src to dst atomically: either both balances change or neither does. Many threads call transfer concurrently, including A to B and B to A at the same moment. The sum of all b...

High-Level Design

The original code does with src.lock: with dst.lock:. Thread 1 holds A and waits for B while thread 2 holds B and waits for A. All four Coffman conditions hold: mutual exclusion, hold and wait, no p...


Submit Your Answer
Markdown supported