Account Balance Tracker

Financial Ledger

Design accounts with deposits, withdrawals and transfers that stay correct when many threads run them at once, and never lose or invent a cent.

Overview

An account balance tracker holds a balance per account and supports three operations: deposit, withdraw, and transfer between two accounts. Each one is a few lines. The problem is that they run at the same time from many threads, and money has an invariant that concurrency loves to break: transfers move value around, they never create or destroy it, so the sum of all balances only changes when money enters or leaves the system.

The interview tests three habits. Money is stored as whole cents, never as a float, because binary floating point cannot hold most decimal fractions exactly. A balance check and the debit that follows it are one atomic step under a lock, otherwise two withdrawals can both pass the check. And a transfer takes two locks, so it must take them in a fixed order, or two opposite transfers can each hold one lock and wait forever for the other.

A strong answer gets all three right in the first version of the code, records every operation as an immutable transaction, and can show a test in which threads hammer two accounts in both directions while the total stays constant.

Scoping the problem

Ask six questions before you write code. Each answer settles a design choice, and the answers below are the ones the code uses.

  • What is the unit of money? Floats drift; cents as integers do not. The code uses integer cents.
  • Can a balance go negative? This decides whether withdraw and transfer need a check. No: an overdraft is refused.
  • Is a transfer to the same account allowed? It would take the same lock twice, so it is refused.
  • How many threads? This decides between one global lock and a lock per account. There are many, so each account gets its own lock.
  • What must the history show? A record per operation, shared between both sides of a transfer. The code keeps immutable transactions with a unique id.
  • Does the total need to be exact at any instant? A consistent snapshot needs every lock at once. Yes: total_cents() holds all locks.

Functional requirements

  • Open an account with an id and an optional starting balance; a duplicate id is refused
  • Deposit a positive number of cents and record a DEPOSIT transaction
  • Withdraw cents when the balance covers them, else raise InsufficientFunds and change nothing
  • Transfer cents between two different accounts atomically: both balances change or neither does
  • Every operation returns an immutable Transaction with a unique id; a transfer appears in both histories
  • Query one balance, one history, or the total of all balances as a consistent snapshot

Non-functional requirements

  • Money is integer cents; no float ever touches a balance
  • Operations on different accounts run in parallel: one lock per account, not one for the tracker
  • Transfers cannot deadlock: locks are always taken in account-id order
  • A transaction record cannot be edited after creation
  • Invalid amounts (zero, negative, non-integer, bool) are rejected before any lock is taken

Interview questions

  • Show me the race in a naive withdraw, then the fix.
  • Two transfers in opposite directions deadlock. Explain it and fix it.
  • How do you extend transfer to a payment that touches several accounts at once?
  • One global lock would be simpler. When is a lock per account worth it?
  • How would this look with a database instead of in-memory locks?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio