Implement a banking system that rejects invalid operations without changing any balance
Company: Yext
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a simple banking system that manages customer accounts identified by unique integer account numbers. Each account stores a current balance in whole-number currency units.
The system must support creating accounts, depositing funds, withdrawing funds, and transferring funds between two accounts. Every operation must be validated: invalid requests (non-existent accounts, non-positive amounts, or insufficient funds) must be rejected and must not modify any balance.
Each mutating operation returns a boolean indicating whether the operation succeeded. The system also supports querying the balance of a given account.
Implement a `Bank` class with the following methods:
- `createAccount(accountId: int, initialBalance: int) -> bool`: create a new account with the given id and initial balance. Returns `False` if the account already exists or the balance is negative.
- `deposit(accountId: int, amount: int) -> bool`: deposit `amount` into the account. Returns `False` if the account does not exist or `amount` is not positive.
- `withdraw(accountId: int, amount: int) -> bool`: withdraw `amount` from the account. Returns `False` if the account does not exist, `amount` is not positive, or the balance is insufficient.
- `transfer(fromId: int, toId: int, amount: int) -> bool`: transfer `amount` from one account to another atomically. Returns `False` if either account is missing, the ids are equal, `amount` is not positive, or the source 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"]
[[], [1, 100], [2, 50], [1, 25], [2, 10], [1, 2, 30], [1], [2]]
Output: [null, true, true, true, true, true, 95, 70]
Explanation:
bank = Bank() # empty banking system
bank.createAccount(1, 100) # -> True (account 1 created with balance 100)
bank.createAccount(2, 50) # -> True (account 2 created with balance 50)
bank.deposit(1, 25) # -> True (account 1 balance: 125)
bank.withdraw(2, 10) # -> True (account 2 balance: 40)
bank.transfer(1, 2, 30) # -> True (account 1: 95, account 2: 70)
bank.getBalance(1) # -> 95
bank.getBalance(2) # -> 70
```
Example 2:
```text
Input:
["Bank", "createAccount", "createAccount", "withdraw", "transfer", "deposit", "getBalance", "getBalance"]
[[], [10, 100], [10, 200], [10, 500], [10, 99, 20], [99, 50], [10], [99]]
Output: [null, true, false, false, false, false, 100, -1]
Explanation:
bank = Bank() # empty banking system
bank.createAccount(10, 100) # -> True (account 10 created)
bank.createAccount(10, 200) # -> False (duplicate id rejected)
bank.withdraw(10, 500) # -> False (insufficient funds, balance unchanged)
bank.transfer(10, 99, 20) # -> False (account 99 does not exist)
bank.deposit(99, 50) # -> False (account 99 does not exist)
bank.getBalance(10) # -> 100 (unchanged after failed ops)
bank.getBalance(99) # -> -1 (no such account)
```
```hint Failure paths first
For each method, write down every condition that makes it fail. A call that returns False should look, from the outside, exactly like a call that never happened.
```
### Constraints & Assumptions
- `1 <= accountId <= 10^9`
- `0 <= initialBalance <= 10^9`
- `1 <= amount <= 10^9` for deposit, withdraw and transfer.
- At most `10^5` operations are performed across all methods.
- `createAccount` may be called with an id that already exists. That call returns `False` and leaves the existing account as it was.
### Clarifying Questions to Ask
- Is a boolean enough for a failed call, or does the caller need to know the reason (missing account, bad amount, insufficient funds)?
- Does "atomically" mean safe under concurrent calls, or only all-or-nothing within a single call?
- Is there an upper limit on a balance, or must it hold values beyond the 32-bit range?
### What a Strong Answer Covers
- The storage structure for accounts and why it fits ids that range up to `10^9`.
- The complete set of failure conditions for each method, including duplicate ids, non-positive amounts and equal ids in a transfer.
- How the code guarantees that a rejected call changes no balance, and what atomic means for `transfer` in this setting.
- Time and space complexity per operation, and the integer width balances need.
- Tests that exercise each failure path and check the balances afterward.
### Follow-up Questions
- How would you make the system thread-safe so that concurrent transfers between the same two accounts can neither deadlock nor lose money?
- A teammate's `transfer` debits the source, then looks up the destination and returns `False` if it is missing. What goes wrong, and which test would catch it?
- How would you report the reason for a failure without breaking callers that expect a boolean?
- How would you add an audit log of successful operations and use it to rebuild balances after a crash?
Overview: Implement an in-memory banking system with caller-chosen account ids that creates accounts, takes deposits and withdrawals, moves money between accounts and answers balance queries. It tests input validation, all-or-nothing transfers, and making sure a rejected call never changes a balance.