Design a Banking System API with Users, Accounts and Transfers

Quick Overview

Implement an in-memory banking system with users, accounts, deposits, withdrawals and transfers, where each successful operation records a transaction with an auto-incrementing id. It tests class design, validation that leaves state unchanged when a call is rejected, and constant-time lookups.

Design a Banking System API with Users, Accounts and Transfers

Company: Ramp

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a simple banking system that manages `User`, `Account` and `Transaction` entities. Each user can own one or more accounts, and each account tracks its own balance along with a chronological list of transactions. The system must support creating users, opening accounts, depositing and withdrawing funds, and transferring funds between two accounts. Every transfer must pass basic validation: both accounts must exist, the amount must be positive, and the source account must have sufficient balance. If any validation fails, the transfer is rejected and no balances are modified. Each successful operation records a `Transaction` (with a unique auto-incrementing id starting at `1`) on the affected account or accounts. A transfer records a single transaction id that is appended to both the source and destination account histories. Implement the following methods: - `createUser(userId: int, name: str) -> bool`: create a new user. Returns `True` if created, or `False` if a user with `userId` already exists. - `openAccount(accountId: int, userId: int) -> bool`: open a new account with balance 0 owned by `userId`. Returns `True` on success, or `False` if the account already exists or the user does not exist. - `deposit(accountId: int, amount: int) -> bool`: deposit `amount` into `accountId`. Returns `True` on success, or `False` if the account does not exist or `amount <= 0`. - `withdraw(accountId: int, amount: int) -> bool`: withdraw `amount` from `accountId`. Returns `True` on success, or `False` if the account does not exist, `amount <= 0`, or the balance is insufficient. - `transfer(fromId: int, toId: int, amount: int) -> bool`: transfer `amount` from `fromId` to `toId`. Returns `True` on success, or `False` if either account does not exist, `amount <= 0`, `fromId == toId`, or the source balance is insufficient. - `getBalance(accountId: int) -> int`: return the current balance of `accountId`, or `-1` if the account does not exist. - `getTransactionCount(accountId: int) -> int`: return the number of transactions recorded on `accountId`, or `-1` if the account does not exist. An example call sequence, with the value each call returns: ```text createUser(1, "Alice") -> True createUser(1, "Bob") -> False openAccount(100, 1) -> True openAccount(200, 1) -> True openAccount(300, 2) -> False deposit(100, 500) -> True withdraw(100, 600) -> False transfer(100, 200, 200) -> True transfer(200, 200, 50) -> False transfer(100, 999, 10) -> False deposit(200, 0) -> False withdraw(200, 50) -> True getBalance(100) -> 300 getBalance(200) -> 150 getTransactionCount(100) -> 2 getTransactionCount(200) -> 2 getBalance(999) -> -1 ``` Account 100 gets transaction 1 (the deposit of 500) and transaction 2 (the transfer of 200 out), so it ends with a balance of 300 and two transactions. Account 200 shares transaction 2 and then gets transaction 3 from the withdrawal of 50, leaving 150. The rejected calls (duplicate user, unknown user, overdraft, self-transfer, unknown destination, zero deposit) change no balance and use no transaction id. ```hint Order of checks For each method, decide which conditions can reject the call and run all of them before you change any balance. Then a failed call has nothing to undo. ``` ```hint One owner for ids Put the transaction counter in a single place that every successful operation goes through, and think about when in the method it should advance. ``` ### Constraints & Assumptions - `1 <= userId, accountId <= 10⁹` - `0 <= amount <= 10⁹` - At most `10⁴` calls are made in total across all methods. ### Clarifying Questions to Ask - Does a rejected call consume a transaction id, or do ids advance only on successful operations? - Is the system called from a single thread, or must a transfer stay correct under concurrent calls? - Should the system keep full transaction records (type, amount, source, destination), or only what the API exposes today? - Can accounts be closed or users removed, and if so, what happens to their transaction history? ### What a Strong Answer Covers - A class design for users, accounts, transactions and the system object, with each piece of state owned by one class. - Lookup structures that keep every operation at constant time on average. - The validation order in each method, and why a rejected call leaves balances, histories and the id counter unchanged. - How one transfer produces a single transaction that both account histories reference, and how the transaction count follows from that. - Edge cases such as zero amounts, unknown ids, self-transfers and balances that outgrow a 32-bit integer. ### Follow-up Questions - How would you make `transfer` correct when many threads call it at once, without deadlocking on the two account locks? - How would you add a method that returns an account's most recent `k` transactions, or a statement for a date range? - How would you support reversing a transfer, and what should happen if the destination has already spent the money? - What changes if the data lives in a database shared by several service instances?

Overview: Implement an in-memory banking system with users, accounts, deposits, withdrawals and transfers, where each successful operation records a transaction with an auto-incrementing id. It tests class design, validation that leaves state unchanged when a call is rejected, and constant-time lookups.

Ramp logo
Ramp
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a simple banking system that manages User, Account and Transaction entities. Each user can own one or more accounts, and each account tracks its own balance along with a chronological list of transactions.

The system must support creating users, opening accounts, depositing and withdrawing funds, and transferring funds between two accounts. Every transfer must pass basic validation: both accounts must exist, the amount must be positive, and the source account must have sufficient balance. If any validation fails, the transfer is rejected and no balances are modified.

Each successful operation records a Transaction (with a unique auto-incrementing id starting at 1) on the affected account or accounts. A transfer records a single transaction id that is appended to both the source and destination account histories.

Implement the following methods:

  • createUser(userId: int, name: str) -> bool : create a new user. Returns True if created, or False if a user with userId already exists.
  • openAccount(accountId: int, userId: int) -> bool : open a new account with balance 0 owned by userId . Returns True on success, or False if the account already exists or the user does not exist.
  • deposit(accountId: int, amount: int) -> bool : deposit amount into accountId . Returns True on success, or False if the account does not exist or amount <= 0 .
  • withdraw(accountId: int, amount: int) -> bool : withdraw amount from accountId . Returns True on success, or False if the account does not exist, amount <= 0 , or the balance is insufficient.
  • transfer(fromId: int, toId: int, amount: int) -> bool : transfer amount from fromId to toId . Returns True on success, or False if either account does not exist, amount <= 0 , fromId == toId , or the source balance is insufficient.
  • getBalance(accountId: int) -> int : return the current balance of accountId , or -1 if the account does not exist.
  • getTransactionCount(accountId: int) -> int : return the number of transactions recorded on accountId , or -1 if the account does not exist.

An example call sequence, with the value each call returns:

createUser(1, "Alice")       -> True
createUser(1, "Bob")         -> False
openAccount(100, 1)          -> True
openAccount(200, 1)          -> True
openAccount(300, 2)          -> False
deposit(100, 500)            -> True
withdraw(100, 600)           -> False
transfer(100, 200, 200)      -> True
transfer(200, 200, 50)       -> False
transfer(100, 999, 10)       -> False
deposit(200, 0)              -> False
withdraw(200, 50)            -> True
getBalance(100)              -> 300
getBalance(200)              -> 150
getTransactionCount(100)     -> 2
getTransactionCount(200)     -> 2
getBalance(999)              -> -1

Account 100 gets transaction 1 (the deposit of 500) and transaction 2 (the transfer of 200 out), so it ends with a balance of 300 and two transactions. Account 200 shares transaction 2 and then gets transaction 3 from the withdrawal of 50, leaving 150. The rejected calls (duplicate user, unknown user, overdraft, self-transfer, unknown destination, zero deposit) change no balance and use no transaction id.

Constraints & Assumptions

  • 1 <= userId, accountId <= 10⁹
  • 0 <= amount <= 10⁹
  • At most 10⁴ calls are made in total across all methods.

Clarifying Questions to Ask Guidance

  • Does a rejected call consume a transaction id, or do ids advance only on successful operations?
  • Is the system called from a single thread, or must a transfer stay correct under concurrent calls?
  • Should the system keep full transaction records (type, amount, source, destination), or only what the API exposes today?
  • Can accounts be closed or users removed, and if so, what happens to their transaction history?

What a Strong Answer Covers Guidance

  • A class design for users, accounts, transactions and the system object, with each piece of state owned by one class.
  • Lookup structures that keep every operation at constant time on average.
  • The validation order in each method, and why a rejected call leaves balances, histories and the id counter unchanged.
  • How one transfer produces a single transaction that both account histories reference, and how the transaction count follows from that.
  • Edge cases such as zero amounts, unknown ids, self-transfers and balances that outgrow a 32-bit integer.

Follow-up Questions Guidance

  • How would you make transfer correct when many threads call it at once, without deadlocking on the two account locks?
  • How would you add a method that returns an account's most recent k transactions, or a statement for a date range?
  • How would you support reversing a transfer, and what should happen if the destination has already spent the money?
  • What changes if the data lives in a database shared by several service instances?