Design a Central Banking Ledger with Deposits, Withdrawals and Transfers

Quick Overview

Implement a central banking ledger that creates accounts, applies deposits and withdrawals, and transfers funds without letting any balance go below zero. It tests validation against an exact return contract, conservation of total money across transfers, and the choice of data structure.

Design a Central Banking Ledger with Deposits, Withdrawals and Transfers

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a central banking ledger system that manages accounts and processes monetary transactions between them. The system must support creating accounts with initial deposits, crediting (depositing) and debiting (withdrawing) individual accounts, and transferring funds between accounts. No account balance may ever go below zero. Credits (deposits) add new money into the system. Debits (withdrawals) remove money from the system. Transfers move money between accounts, so the total sum of money in the system remains constant: money is neither created nor destroyed during a transfer. Implement the following methods: - `create_account(name: str, initial_deposit: int) -> bool`: create a new account with the given name and initial deposit. Returns `True` if the account was created successfully, `False` if the account already exists or the deposit is negative. - `deposit(name: str, amount: int) -> bool`: deposit (credit) the given amount into the specified account. Returns `True` on success, `False` if the account does not exist or the amount is negative. - `withdraw(name: str, amount: int) -> bool`: withdraw (debit) the given amount from the specified account. Returns `True` on success, `False` if the account does not exist, the amount is negative, or the account has insufficient funds. - `transfer(from_account: str, to_account: str, amount: int) -> bool`: transfer the given amount from one account to another. Returns `True` on success, `False` if either account does not exist, they are the same account, the amount is negative, or the sender has insufficient funds. The total money in the system is preserved. - `get_balance(name: str) -> int`: return the current balance of the specified account. Returns `-1` if the account does not exist. - `get_total_money() -> int`: return the total sum of money across all accounts in the system. In this first call sequence, each line shows the value the call returns: ```text ledger = CentralBankingLedger() ledger.create_account("Alice", 100) -> True ledger.create_account("Bob", 50) -> True ledger.deposit("Bob", 25) -> True ledger.transfer("Alice", "Bob", 30) -> True ledger.transfer("Bob", "Bob", 10) -> False ledger.withdraw("Alice", 70) -> True ledger.deposit("Carol", 10) -> False ledger.withdraw("Bob", -5) -> False ledger.get_balance("Alice") -> 0 ledger.get_balance("Bob") -> 105 ledger.get_total_money() -> 105 ``` The deposit raises the total from 150 to 175. The transfer moves 30 from Alice (now 70) to Bob (now 105) and leaves the total at 175. Withdrawing Alice's full 70 brings the total down to 105. The self-transfer, the deposit into the missing account Carol and the negative withdrawal are all rejected. The second sequence: ```text ledger = CentralBankingLedger() ledger.create_account("Alice", 100) -> True ledger.create_account("Alice", 200) -> False ledger.transfer("Alice", "Bob", 50) -> False ledger.withdraw("Alice", 200) -> False ledger.get_balance("Alice") -> 100 ledger.create_account("Bob", 0) -> True ledger.get_balance("Bob") -> 0 ``` The second `create_account` fails because Alice already exists. The transfer fails because Bob does not exist yet, and the withdrawal of 200 fails because Alice has only 100, so her balance stays at 100. Creating Bob with an initial deposit of 0 is allowed. ```hint Check before you write List every condition that makes a call fail, including the rule that no balance may drop below zero, and make sure a failed call leaves every balance exactly as it was. ``` ### Constraints & Assumptions - `1 <= name.length <= 20` - `0 <= initial_deposit, amount <= 10⁶` - Account names consist of alphanumeric characters. - At most `10⁴` calls will be made in total. ### Clarifying Questions to Ask - Is an amount of 0 valid for deposits, withdrawals and transfers, or should it be rejected like a negative amount? - Are account names case-sensitive, so that "alice" and "Alice" are different accounts? - Is the ledger called from a single thread, or can transfers run concurrently? - Does the ledger need a history of individual transactions, or only current balances? ### What a Strong Answer Covers - A data structure that keeps every per-account operation at constant time on average. - Validation in each method that matches the return contract exactly, including zero amounts, self-transfers and withdrawing the exact balance. - Why a successful transfer preserves the total money and why a failed one changes neither balance. - How `get_total_money` is computed and what it costs, including the option of keeping a running total. - The integer range of balances and totals under the given constraints. ### Follow-up Questions - How would you keep transfers correct when many threads call the ledger at once, and how do you avoid deadlock? - How would you record a transaction history so that any balance can be audited or rebuilt? - How would you turn this into a double-entry ledger, and what invariant would you check? - How would you support accounts that hold several currencies?

Overview: Implement a central banking ledger that creates accounts, applies deposits and withdrawals, and transfers funds without letting any balance go below zero. It tests validation against an exact return contract, conservation of total money across transfers, and the choice of data structure.

Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a central banking ledger system that manages accounts and processes monetary transactions between them.

The system must support creating accounts with initial deposits, crediting (depositing) and debiting (withdrawing) individual accounts, and transferring funds between accounts. No account balance may ever go below zero.

Credits (deposits) add new money into the system. Debits (withdrawals) remove money from the system. Transfers move money between accounts, so the total sum of money in the system remains constant: money is neither created nor destroyed during a transfer.

Implement the following methods:

  • create_account(name: str, initial_deposit: int) -> bool : create a new account with the given name and initial deposit. Returns True if the account was created successfully, False if the account already exists or the deposit is negative.
  • deposit(name: str, amount: int) -> bool : deposit (credit) the given amount into the specified account. Returns True on success, False if the account does not exist or the amount is negative.
  • withdraw(name: str, amount: int) -> bool : withdraw (debit) the given amount from the specified account. Returns True on success, False if the account does not exist, the amount is negative, or the account has insufficient funds.
  • transfer(from_account: str, to_account: str, amount: int) -> bool : transfer the given amount from one account to another. Returns True on success, False if either account does not exist, they are the same account, the amount is negative, or the sender has insufficient funds. The total money in the system is preserved.
  • get_balance(name: str) -> int : return the current balance of the specified account. Returns -1 if the account does not exist.
  • get_total_money() -> int : return the total sum of money across all accounts in the system.

In this first call sequence, each line shows the value the call returns:

ledger = CentralBankingLedger()
ledger.create_account("Alice", 100)     -> True
ledger.create_account("Bob", 50)        -> True
ledger.deposit("Bob", 25)               -> True
ledger.transfer("Alice", "Bob", 30)     -> True
ledger.transfer("Bob", "Bob", 10)       -> False
ledger.withdraw("Alice", 70)            -> True
ledger.deposit("Carol", 10)             -> False
ledger.withdraw("Bob", -5)              -> False
ledger.get_balance("Alice")             -> 0
ledger.get_balance("Bob")               -> 105
ledger.get_total_money()                -> 105

The deposit raises the total from 150 to 175. The transfer moves 30 from Alice (now 70) to Bob (now 105) and leaves the total at 175. Withdrawing Alice's full 70 brings the total down to 105. The self-transfer, the deposit into the missing account Carol and the negative withdrawal are all rejected.

The second sequence:

ledger = CentralBankingLedger()
ledger.create_account("Alice", 100)     -> True
ledger.create_account("Alice", 200)     -> False
ledger.transfer("Alice", "Bob", 50)     -> False
ledger.withdraw("Alice", 200)           -> False
ledger.get_balance("Alice")             -> 100
ledger.create_account("Bob", 0)         -> True
ledger.get_balance("Bob")               -> 0

The second create_account fails because Alice already exists. The transfer fails because Bob does not exist yet, and the withdrawal of 200 fails because Alice has only 100, so her balance stays at 100. Creating Bob with an initial deposit of 0 is allowed.

Constraints & Assumptions

  • 1 <= name.length <= 20
  • 0 <= initial_deposit, amount <= 10⁶
  • Account names consist of alphanumeric characters.
  • At most 10⁴ calls will be made in total.

Clarifying Questions to Ask Guidance

  • Is an amount of 0 valid for deposits, withdrawals and transfers, or should it be rejected like a negative amount?
  • Are account names case-sensitive, so that "alice" and "Alice" are different accounts?
  • Is the ledger called from a single thread, or can transfers run concurrently?
  • Does the ledger need a history of individual transactions, or only current balances?

What a Strong Answer Covers Guidance

  • A data structure that keeps every per-account operation at constant time on average.
  • Validation in each method that matches the return contract exactly, including zero amounts, self-transfers and withdrawing the exact balance.
  • Why a successful transfer preserves the total money and why a failed one changes neither balance.
  • How get_total_money is computed and what it costs, including the option of keeping a running total.
  • The integer range of balances and totals under the given constraints.

Follow-up Questions Guidance

  • How would you keep transfers correct when many threads call the ledger at once, and how do you avoid deadlock?
  • How would you record a transaction history so that any balance can be audited or rebuilt?
  • How would you turn this into a double-entry ledger, and what invariant would you check?
  • How would you support accounts that hold several currencies?