Design a bank system with transfers
MetaSoftware Engineer · HR ScreenLast updated
This question evaluates system-design and transactional engineering skills for financial services, including API design, data modeling, correctness under holds and transfer expirations, idempotency, merge semantics, and complexity reasoning.
Design and implement a single-process BankSystem that supports the following APIs (timestamps are monotonically increasing integers; amounts are non‑negative integers). Provide data models and algorithms to satisfy the behaviors and edge cases: (
- bool createAccount(timestamp, customerId); (
- int deposit(timestamp, customerId, amount); (
- int pay(timestamp, sourceAccountId, targetAccountId, amount); (
- String transfer(timestamp, targetAccountId, amount) — returns a unique transferId and immediately places a hold on the source account’s funds; (
- boolean accept(timestamp, accountId, transferId); (
- void mergeCustomers(oldId, newId). Requirements: (a) For levels 1–3, assume one account per customer (accountId == customerId). A transfer places a hold and deducts the amount from the source at transfer time; accept within an expiry window completes the transfer by crediting the target; if a transfer expires before acceptance, the held funds must be returned to the source. (b) All balance‑mutating operations (deposit and pay) must first detect and unwind any expired transfers affecting the involved accounts so that their return values reflect the correct post‑expiration balances. (c) On successful acceptance, the transfer amount must contribute to the account’s activity metric (used by the Top‑N query); expired transfers do not. (d) Define precise return values and errors for nonexistent accounts, insufficient funds, duplicate or late accept calls, invalid/expired transferIds, and idempotency expectations. (e) mergeCustomers(oldId, newId) merges customer identities while preserving each original account’s balance and full transaction history; reassign all of oldId’s accounts and any open/held/settled transfers to newId without altering historical records. Explain your chosen data structures, the time/space complexity for each API, and how you would test long mixed operation sequences with many accounts.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.