Design a ledger with deposits, withdrawals, transfers and per-account history

Quick Overview

Design a ledger that handles deposits, withdrawals and transfers between accounts, rejects overdrafts and self-transfers, and reports balances and per-account history. It tests the order of validation and writes, how a transfer appears on both accounts, and the cost of each method.

Design a ledger with deposits, withdrawals, transfers and per-account history

Company: Monzo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a backend ledger that records financial transactions between accounts and supports balance and history queries. The ledger supports three transaction types: deposit (credit to an account), withdraw (debit from an account), and transfer (debit one account and credit another). A withdrawal or transfer must be rejected if the source account has insufficient funds. Each accepted transaction is assigned a unique, monotonically increasing `transaction_id` (starting at `1`) and stored in the history of every account it affects. A rejected call changes nothing: no balance moves, no history entry is added, and no ID is used up. Accounts are created on first reference with a starting balance of `0`. Implement the following methods: - `deposit(account_id: str, amount: int) -> int`: credit the given amount to the account. Returns the assigned transaction ID. - `withdraw(account_id: str, amount: int) -> int`: debit the given amount from the account. Returns the assigned transaction ID, or -1 if the account has insufficient funds. - `transfer(from_id: str, to_id: str, amount: int) -> int`: move the given amount from one account to another. Returns the assigned transaction ID, or -1 if the source has insufficient funds or the accounts are the same. - `get_balance(account_id: str) -> int`: return the current balance of the account (0 if it has never been referenced). - `get_history(account_id: str) -> list`: return the chronological list of transactions affecting the account. Each entry is `[transaction_id, type, amount, counterparty]`, where `type` is `"deposit"`, `"withdraw"`, `"transfer_out"` (on the source account of a transfer) or `"transfer_in"` (on the destination), and `counterparty` is the other account for transfers or an empty string otherwise. The first example shows two rejected calls and a transfer of the full balance: ```text Input: ["Ledger", "deposit", "withdraw", "withdraw", "transfer", "transfer", "get_balance", "get_history"] [[], ["alice", 100], ["alice", 30], ["bob", 10], ["alice", "bob", 80], ["alice", "bob", 70], ["alice"], ["alice"]] Output: [null, 1, 2, -1, -1, 3, 0, [[1, "deposit", 100, ""], [2, "withdraw", 30, ""], [3, "transfer_out", 70, "bob"]]] Explanation: ledger = Ledger() # empty ledger ledger.deposit("alice", 100) # -> 1 (alice: 100) ledger.withdraw("alice", 30) # -> 2 (alice: 70) ledger.withdraw("bob", 10) # -> -1 (bob has 0, rejected) ledger.transfer("alice", "bob", 80) # -> -1 (alice has only 70, rejected) ledger.transfer("alice", "bob", 70) # -> 3 (alice: 0, bob: 70) ledger.get_balance("alice") # -> 0 ledger.get_history("alice") # -> the three accepted transactions, oldest first ``` The second example shows an unknown account, a rejected self-transfer, and the destination side of a transfer: ```text Input: ["Ledger", "get_balance", "transfer", "deposit", "transfer", "get_balance", "get_history"] [[], ["ghost"], ["x", "x", 10], ["x", 20], ["x", "y", 5], ["y"], ["y"]] Output: [null, 0, -1, 1, 2, 5, [[2, "transfer_in", 5, "x"]]] Explanation: ledger = Ledger() # empty ledger ledger.get_balance("ghost") # -> 0 (unknown account) ledger.transfer("x", "x", 10) # -> -1 (self-transfer rejected) ledger.deposit("x", 20) # -> 1 (x: 20) ledger.transfer("x", "y", 5) # -> 2 (x: 15, y: 5) ledger.get_balance("y") # -> 5 ledger.get_history("y") # -> [[2, "transfer_in", 5, "x"]] ``` ```hint Order the checks and the writes A rejected call must leave balances, histories and the ID counter exactly as they were. Decide which checks run before any state changes, and at which point the next ID is taken. ``` ### Constraints & Assumptions - `1 <= amount <= 10⁹` - Account IDs are non-empty strings of length up to `32`. - At most `10⁵` calls will be made across all methods. - Transfers where the source and destination are the same account are rejected. ### Clarifying Questions to Ask - Is the ledger called from a single thread, or must a transfer stay correct while other calls run at the same time? - Should callers be able to change the ledger by editing the list that `get_history` returns? - Can the amount constraint be trusted, or should the ledger reject zero or negative amounts itself? - Do rejected attempts need to be logged anywhere, for example for fraud review, even though they stay out of the history? ### What a Strong Answer Covers - The state the ledger keeps (balances, per-account histories, an ID counter) and the cost of each method. - The order of checks and writes that leaves balances, histories and the counter untouched on every rejected call. - How a transfer is recorded on both accounts under one ID, and where the self-transfer check belongs. - Read behavior for unknown accounts, and a history result that callers cannot use to alter stored entries. - Integer width for balances in languages with fixed-size integers. ### Follow-up Questions - How would you make `transfer` correct when many threads call the ledger at once, without letting two opposite transfers deadlock? - Amounts now arrive as decimal currency values such as 10.25. How do you represent and add them? - How would you make `deposit` safe to retry when a client resends the same request after a timeout? - Some accounts have millions of history entries. How would you serve the history one page at a time?

Overview: Design a ledger that handles deposits, withdrawals and transfers between accounts, rejects overdrafts and self-transfers, and reports balances and per-account history. It tests the order of validation and writes, how a transfer appears on both accounts, and the cost of each method.

Monzo logo
Monzo
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a backend ledger that records financial transactions between accounts and supports balance and history queries.

The ledger supports three transaction types: deposit (credit to an account), withdraw (debit from an account), and transfer (debit one account and credit another). A withdrawal or transfer must be rejected if the source account has insufficient funds.

Each accepted transaction is assigned a unique, monotonically increasing transaction_id (starting at 1) and stored in the history of every account it affects. A rejected call changes nothing: no balance moves, no history entry is added, and no ID is used up. Accounts are created on first reference with a starting balance of 0.

Implement the following methods:

  • deposit(account_id: str, amount: int) -> int : credit the given amount to the account. Returns the assigned transaction ID.
  • withdraw(account_id: str, amount: int) -> int : debit the given amount from the account. Returns the assigned transaction ID, or -1 if the account has insufficient funds.
  • transfer(from_id: str, to_id: str, amount: int) -> int : move the given amount from one account to another. Returns the assigned transaction ID, or -1 if the source has insufficient funds or the accounts are the same.
  • get_balance(account_id: str) -> int : return the current balance of the account (0 if it has never been referenced).
  • get_history(account_id: str) -> list : return the chronological list of transactions affecting the account. Each entry is [transaction_id, type, amount, counterparty] , where type is "deposit" , "withdraw" , "transfer_out" (on the source account of a transfer) or "transfer_in" (on the destination), and counterparty is the other account for transfers or an empty string otherwise.

The first example shows two rejected calls and a transfer of the full balance:

Input:
["Ledger", "deposit", "withdraw", "withdraw", "transfer", "transfer", "get_balance", "get_history"]
[[], ["alice", 100], ["alice", 30], ["bob", 10], ["alice", "bob", 80], ["alice", "bob", 70], ["alice"], ["alice"]]

Output: [null, 1, 2, -1, -1, 3, 0, [[1, "deposit", 100, ""], [2, "withdraw", 30, ""], [3, "transfer_out", 70, "bob"]]]

Explanation:
ledger = Ledger()                      # empty ledger
ledger.deposit("alice", 100)           # -> 1   (alice: 100)
ledger.withdraw("alice", 30)           # -> 2   (alice: 70)
ledger.withdraw("bob", 10)             # -> -1  (bob has 0, rejected)
ledger.transfer("alice", "bob", 80)    # -> -1  (alice has only 70, rejected)
ledger.transfer("alice", "bob", 70)    # -> 3   (alice: 0, bob: 70)
ledger.get_balance("alice")            # -> 0
ledger.get_history("alice")            # -> the three accepted transactions, oldest first

The second example shows an unknown account, a rejected self-transfer, and the destination side of a transfer:

Input:
["Ledger", "get_balance", "transfer",     "deposit", "transfer",    "get_balance", "get_history"]
[[],       ["ghost"],     ["x", "x", 10], ["x", 20], ["x", "y", 5], ["y"],         ["y"]]

Output: [null, 0, -1, 1, 2, 5, [[2, "transfer_in", 5, "x"]]]

Explanation:
ledger = Ledger()                      # empty ledger
ledger.get_balance("ghost")            # -> 0   (unknown account)
ledger.transfer("x", "x", 10)          # -> -1  (self-transfer rejected)
ledger.deposit("x", 20)                # -> 1   (x: 20)
ledger.transfer("x", "y", 5)           # -> 2   (x: 15, y: 5)
ledger.get_balance("y")                # -> 5
ledger.get_history("y")                # -> [[2, "transfer_in", 5, "x"]]

Constraints & Assumptions

  • 1 <= amount <= 10⁹
  • Account IDs are non-empty strings of length up to 32 .
  • At most 10⁵ calls will be made across all methods.
  • Transfers where the source and destination are the same account are rejected.

Clarifying Questions to Ask Guidance

  • Is the ledger called from a single thread, or must a transfer stay correct while other calls run at the same time?
  • Should callers be able to change the ledger by editing the list that get_history returns?
  • Can the amount constraint be trusted, or should the ledger reject zero or negative amounts itself?
  • Do rejected attempts need to be logged anywhere, for example for fraud review, even though they stay out of the history?

What a Strong Answer Covers Guidance

  • The state the ledger keeps (balances, per-account histories, an ID counter) and the cost of each method.
  • The order of checks and writes that leaves balances, histories and the counter untouched on every rejected call.
  • How a transfer is recorded on both accounts under one ID, and where the self-transfer check belongs.
  • Read behavior for unknown accounts, and a history result that callers cannot use to alter stored entries.
  • Integer width for balances in languages with fixed-size integers.

Follow-up Questions Guidance

  • How would you make transfer correct when many threads call the ledger at once, without letting two opposite transfers deadlock?
  • Amounts now arrive as decimal currency values such as 10.25. How do you represent and add them?
  • How would you make deposit safe to retry when a client resends the same request after a timeout?
  • Some accounts have millions of history entries. How would you serve the history one page at a time?