Implement an in-memory bank with accounts, deposits, withdrawals and transfers
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a simple banking application that supports creating accounts, depositing and withdrawing funds, transferring money between accounts, and querying balances.
Accounts are identified by a positive integer `accountId` starting from `1`, assigned sequentially as new accounts are created. The first account created has `accountId = 1`, the second has `accountId = 2`, and so on.
All monetary amounts and balances are non-negative integers. A withdrawal or transfer must fail (return `False`) if the source account does not exist, the destination account does not exist (for transfers), or there are insufficient funds. Successful mutations return `True`.
Implement a `Bank` class with the following methods:
- `createAccount(initialBalance: int) -> int`: create a new account with the given initial balance and return its newly assigned `accountId`.
- `deposit(accountId: int, amount: int) -> bool`: deposit `amount` into the specified account. Returns `True` on success, `False` if the account does not exist.
- `withdraw(accountId: int, amount: int) -> bool`: withdraw `amount` from the specified account. Returns `False` if the account does not exist or has insufficient funds.
- `transfer(fromId: int, toId: int, amount: int) -> bool`: transfer `amount` from `fromId` to `toId`. Returns `False` if either account does not exist or `fromId` has insufficient funds.
- `getBalance(accountId: int) -> int`: return the current balance of the account, or `-1` if the account does not exist.
Example 1:
```text
Input:
["Bank", "createAccount", "createAccount", "deposit", "withdraw", "transfer", "getBalance", "getBalance"]
[[], [100], [50], [1, 200], [2, 30], [1, 2, 100], [1], [2]]
Output: [null, 1, 2, true, true, true, 200, 120]
Explanation:
bank = Bank() # empty bank
bank.createAccount(100) # -> 1 (account 1 created with balance 100)
bank.createAccount(50) # -> 2 (account 2 created with balance 50)
bank.deposit(1, 200) # -> True (account 1 balance: 300)
bank.withdraw(2, 30) # -> True (account 2 balance: 20)
bank.transfer(1, 2, 100) # -> True (account 1: 200, account 2: 120)
bank.getBalance(1) # -> 200
bank.getBalance(2) # -> 120
```
Example 2:
```text
Input:
["Bank", "createAccount", "withdraw", "withdraw", "transfer", "deposit", "getBalance", "getBalance"]
[[], [50], [1, 100], [1, 50], [1, 5, 10], [5, 25], [1], [5]]
Output: [null, 1, false, true, false, false, 0, -1]
Explanation:
bank = Bank() # empty bank
bank.createAccount(50) # -> 1 (account 1 created with balance 50)
bank.withdraw(1, 100) # -> False (insufficient funds)
bank.withdraw(1, 50) # -> True (account 1 balance: 0)
bank.transfer(1, 5, 10) # -> False (account 5 does not exist)
bank.deposit(5, 25) # -> False (account 5 does not exist)
bank.getBalance(1) # -> 0
bank.getBalance(5) # -> -1 (account 5 does not exist)
```
```hint Order of operations
Think about what state the bank is left in when a transfer fails halfway. Decide which checks have to pass before any balance is changed.
```
### Constraints & Assumptions
- `1 <= initialBalance, amount <= 10^9`
- `1 <= accountId <= 10^5` in every call that takes an id.
- At most `10^5` calls are made to any single method.
- Account ids are assigned sequentially starting from `1`. There is no operation that closes an account.
### Clarifying Questions to Ask
- Should a transfer from an account to itself succeed as a no-op, or be rejected?
- Can `amount` ever be zero, and if so, does a zero-amount call count as a success?
- Is the bank used from a single thread, or must concurrent calls be safe?
- Do failed operations need to be recorded anywhere, or is returning `False` enough?
### What a Strong Answer Covers
- The storage chosen for balances and how the next account id is generated.
- Validation order: which checks run before any mutation, so that a failed call leaves every balance unchanged.
- Transfer semantics, including a missing destination account and a transfer where `fromId == toId`.
- Time and space complexity per operation, and the numeric range balances can reach.
- Boundary cases such as withdrawing the exact balance and querying an id that was never created.
### Follow-up Questions
- How would you make `transfer` safe under concurrent calls without risking a deadlock between two opposite transfers?
- How would you add a transaction history so a caller can list the last k operations on an account?
- How would you add account closing, and what should happen to the closed id and any remaining balance?
- If balances lived in a database, how would you keep a transfer atomic when two withdrawals race on the same account?
Overview: Implement an in-memory bank class that creates accounts with sequential ids and supports deposits, withdrawals, transfers and balance queries. It tests hash map design, validating every condition before changing state, and clean handling of missing accounts and insufficient funds.