Implement Deadlock-Free Concurrent Transfers Between Two Accounts
Last updated: November 29, 2026
Quick Overview
Write a thread-safe transfer(src, dst, amount) for in-memory accounts that never loses money and cannot deadlock when transfers A to B and B to A run at the same time, then explain how you would test it.
J.P. Morgan
November 29, 20260
0
0 solved
Write a thread-safe transfer(src, dst, amount) for in-memory accounts that never loses money and cannot deadlock when transfers A to B and B to A run at the same time, then explain how you would test it.
Concurrency questions are a staple of J.P. Morgan technical screens, especially for Java-heavy core banking and payments teams, and the account transfer is the canonical scenario. The interviewer checks that you lock both accounts, recognize the circular-wait deadlock from naive locking, and fix it with a global lock order instead of timeouts alone.
What the Interviewer Expects
- Explain why locking src then dst deadlocks with an opposite transfer
- Name the four Coffman conditions and which one you break
- Impose a total lock order by account id
- Check funds inside the critical section, not before it
- Write a stress test that asserts total money is conserved
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you do this across two database rows instead of in memory?
- What changes if a transfer touches three or more accounts?
- When would you prefer tryLock with backoff over lock ordering?
- How does Python's GIL affect whether you still need these locks?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Balances are integer cents. A transfer either fully succeeds or raises, total money is conserved, an overdraft is rejected, and concurrent opposite transfers never hang. Same-account and non-positive ...
High-Level Design
A single lock per account is needed so a reader never sees a debit without its credit. Locking src then dst deadlocks when thread 1 holds A and wants B while thread 2 holds B and wants A: mutual e...