Design Bank Accounts with Transaction History
Model customers who hold checking, savings, and credit accounts with different rules, plus each account's transaction history: recent transactions, lookup by day, a customer's latest transaction, and a six-month retention cleanup.
For this exercise, assume you are designing the in-memory classes behind a bank's customer accounts. A customer is identified by a customer ID and is registered before any account is opened for them. A customer can hold any number of accounts. Each account belongs to exactly one customer and has a unique account ID chosen by the bank. There are three kinds of account, and they follow different rules:
- A checking account holds money. A withdrawal is declined if it is larger than the balance. - A savings account also holds money, but a withdrawal is declined if it would leave less than $100.00 in the account. - A credit account has a credit limit set when it is opened, and its balance is the amount the customer owes. A withdrawal is a cash advance that adds to the amount owed; it is declined if the amount owed would go over the limit. A deposit is a payment that lowers the amount owed; it is declined if it is larger than the amount owed.
Every new account starts with a balance of zero. Amounts are whole cents in US dollars and must be positive.
Every deposit or withdrawal that goes through becomes a transaction record: a transaction ID, the account, whether it was a deposit or a withdrawal, the amount, the time it was posted, and the account's balance right after it. Records never change once written.
A declined request is not recorded and changes nothing, and the caller is told why it was declined. The bank handles one request at a time, in time order, and the posting time comes from the bank's own clock.
The bank needs three reads: - The account screen shows an account's current balance and its most recent N transactions, newest first. The app asks for N = 60. - Support staff can list an account's transactions on one calendar day, oldest first.
- The app's home screen shows a customer's single most recent transaction across all of their accounts. This is the most frequent request of all, so it should be answered without looking through the customer's transaction history.
The bank keeps transactions for six months only. Once a day a cleanup removes every transaction posted before the same date six months earlier; for example, the run on 2025-09-15 removes everything posted before 2025-03-15. No read ever returns a removed transaction, and the cleanup never changes a balance.
Cards, purchases at merchants, transfers between accounts, interest, fees, monthly statements, closing accounts, joint accounts, logins, and saving data to disk are out of scope.
Design the classes, what each one owns, how they relate, their public methods, and how a deposit or withdrawal flows through them. A class diagram described in words is enough; no code is needed. Then extend the design for two follow-ups.