Make a Funds Transfer Service Thread Safe Without Deadlocks

Last updated: November 29, 2026

Quick Overview

A banking client's transfer service moves money between in-memory accounts and is called from many threads at once. Make `transfer(src, dst, amount)` correct under concurrency, so balances never go negative or lose money, and so that two opposite transfers can never deadlock.

EPAM Systems
Software Engineering Fundamentals
Software Engineer
EPAM Systems
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


A banking client's transfer service moves money between in-memory accounts and is called from many threads at once. Make `transfer(src, dst, amount)` correct under concurrency, so balances never go negative or lose money, and so that two opposite transfers can never deadlock.

Financial services is one of EPAM's largest verticals, and Java and .NET technical interviews there routinely probe concurrency fundamentals. The interviewer checks that you see the check-then-act race, hold both locks for the critical section, and prevent deadlock with a global lock order.

What the Interviewer Expects
  • Name the race: reading the balance and writing it are separate steps
  • Lock both accounts for the whole check and update
  • Acquire locks in a consistent global order (by account ID) to avoid deadlock
  • Validate inputs: positive amount, distinct accounts
  • Use exact decimal arithmetic and show a stress test that preserves the total
Key Topics to Cover
concurrency
deadlock avoidance
lock ordering
race conditions
idempotency
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 does this change when balances live in a relational database instead of memory?
  • What if account IDs are not comparable, or the same object can appear under two IDs?
  • When would you prefer `tryLock` with timeouts over strict ordering?
  • How would you make transfers idempotent if the caller retries?
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

Invariants: total money is conserved, no balance goes below zero, and the system makes progress. transfer raises on insufficient funds without changing anything. Many threads may transfer A to B and...

High-Level Design

Without locking, two threads can both read a balance of 100, both subtract 80, and both succeed. Locking only the source is not enough because the destination update is also a read-modify-write. Locki...


Submit Your Answer
Markdown supported

Related Questions