Bank System. This is an old question that has shown up on the forum before.
Level 1: implement the createAccount, deposit, and pay methods.
class BankSystemImpl : BankSystem {
bool createAccount(timestamp, customerId)
Int deposit(timestamp, customerId, amount)
Int pay(timestamp, sourceAccountId, targetAccountId, amount)
} ...
Level 2: find the top N activity, based on the sum of deposit and pay amounts. You just maintain a size-N min-heap with a priority queue.
Level 3:
String transfer(timestamp, targetAccountId, amount) // returns transferId
boolean accept(timestamp, accountId, transferId)
When you transfer, you hold the money from the source account; when you accept, it actually moves to the target account.
Level 4: Merge Accounts
mergeCustomers(oldId, newId)
Merge two customers, and you need to preserve each one's balance, transaction history, etc.
I got through 3 levels total, and ran out of time on level 4. The first two levels basically only take 15 minutes to write out completely. I spent too much time debugging level 3. It was actually a few very small bugs, but I only understood what I was missing after working through the test case by hand.
- When you transfer, you remove the amount from the source account. When you accept, if the transaction has expired, you need to put the held money back into the source account.
- If the accept succeeds, that amount needs to be added to the activity amount. Otherwise the top-N test case in level 3 will fail.
- If you deposit/pay after a transaction has expired, you need to make sure the returned amount is correct — both deposit and pay need an extra layer of logic to handle expired transactions. This one took a while to debug because the test case was so long, with 10 accounts each doing all kinds of operations — it took some time just to understand the test itself.
I got into the last question but didn't have time to do it.
Discussion
Loading comments…