Deadlock-Free Concurrent Transfers Between Ledger Accounts
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe transfer(src, dst, amount) on an in-memory ledger where many threads move money between accounts concurrently, guaranteeing no deadlock, no negative balances and conservation of the total.
UBS
November 29, 20260
0
0 solved
Implement a thread-safe transfer(src, dst, amount) on an in-memory ledger where many threads move money between accounts concurrently, guaranteeing no deadlock, no negative balances and conservation of the total.
Core banking and payment services at UBS are heavily concurrent Java systems, and interviewers often probe whether candidates understand locking beyond a single synchronized method. This question checks the classic two-lock deadlock, lock ordering, atomic check-then-act, and how the in-memory answer maps onto a real database.
What the Interviewer Expects
- Explain why locking src then dst deadlocks when two threads transfer in opposite directions
- Impose a global lock order such as account id
- Do the balance check and both updates under the locks
- Use decimal or integer minor units for money
- Write a concurrent test that would hang or break invariants if the code were wrong
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 the same transfer in PostgreSQL, and what isolation level or locking do you need?
- What if accounts live in different services or databases?
- How would you avoid lock contention on a very hot account such as a fee collection account?
- Compare this with an optimistic, version-checked approach.
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
Accounts have ids and Decimal balances. transfer either moves the full amount or raises with no change. Invariants: the sum of balances never changes, no balance goes negative, and no combination of...
High-Level Design
A single global lock is correct but serializes everything. Per-account locks scale, but the obvious version (lock src, then lock dst) deadlocks: thread 1 transfers A to B and holds A, thread 2 transfe...