Anthropic's "Banking System" interview question is a multi-stage coding challenge focused on OOP principles, data structures for account/transaction management, and simulation of banking operations like deposits, withdrawals, and transfers. It appears in their software engineering interviews, often as a live or take-home task that evolves in complexity.[7]
Implement a banking system class (Bank) that manages multiple Account objects. Core requirements include:
` class Account { String id, owner; double balance; List<Transaction> history; // or Queue }
class Bank { Map<String, Account> accounts; boolean createAccount(String id, String owner, double initialBalance); boolean deposit(String id, double amount); boolean withdraw(String id, double amount); boolean transfer(String fromId, String toId, double amount); double getBalance(String id); List<Transaction> getHistory(String id); // Extensions: void applyInterest(double rate); void freezeAccount(String id); } ` Use synchronized methods or Locks for thread-safety in transfers. Transactions are immutable objects with type, amount, timestamp, status (success/fail).[7]
No official examples exist publicly, but reconstructed from reports:
Example 1: Basic Operations ` Input: bank.createAccount("acc1", "Alice", 1000.0) bank.deposit("acc1", 500.0) // Returns true, balance=1500.0 bank.withdraw("acc1", 200.0) // Returns true, balance=1300.0 bank.transfer("acc1", "acc2", 300.0) // First create acc2, returns true
Output:
**Example 2: Failure Case (Simulation)**
Input:
bank.withdraw("acc1", 2000.0) // balance=1300.0Output: false (insufficient funds), history appends failed transaction
**Concurrent Simulation Example:**
Threads:
T1: transfer("acc1", "acc2", 100)
T2: withdraw("acc1", 50) // Race condition possible
Output: One succeeds atomically, other fails; no lost updates. ` // Visual of Bank class UML if available in context; here conceptual.[1]