KatInterview

Bank Ledger with Holds, Expiring Transfers and Customer Merge

Data Structure DesignModel answer
HardModel answerData Structure Designobject-oriented-designhash-mapheap

Last updated

Build an in-memory bank that supports deposits, payments, two-step transfers with expiring holds, a top-activity ranking, and customer merges that leave history untouched.

Design and implement an in-memory, single-threaded BankSystem. Every call carries an integer timestamp. Timestamps never decrease, and amounts are non-negative integers. Explain your data model, the cost of each operation, and how you would test it.

Operations

  1. createAccount(ts, customerId) -> bool
  2. deposit(ts, accountId, amount) -> int
  3. pay(ts, sourceId, targetId, amount) -> int
  4. transfer(ts, sourceId, targetId, amount) -> str: reserves the money on the source right away and returns a unique transfer id.
  5. accept(ts, accountId, transferId) -> bool: the target account confirms the transfer.
  6. mergeCustomers(oldId, newId) -> None
  7. topActivity(ts, n) -> list[str]: ids of the n most active accounts.

The original wording omitted the source from transfer. Assume the signature above includes it.

Rules

  • Accounts. For the first three operations, assume one account per customer, so accountId == customerId.
  • Holds. transfer subtracts the amount from the source immediately. The money sits in the transfer record and cannot be spent elsewhere. accept credits the target.
  • Expiry. A transfer is valid for a fixed window EXPIRY. A transfer created at t0 can be accepted while ts < t0 + EXPIRY. At ts >= t0 + EXPIRY it is expired, and the held amount must go back to the source.
  • Expiry must be visible. Any call that changes or reports a balance (deposit, pay, and so on) must first unwind all expired transfers, so returned balances are always correct.
  • Activity. An account's activity is the sum of its deposits, its outgoing payments, and the amounts of accepted transfers (counted for both source and target). Expired transfers add nothing. topActivity sorts by activity descending, then by id ascending.
  • Merge. mergeCustomers(oldId, newId) moves every account owned by oldId to newId. Balances, transfers (open, held or settled) and history must be preserved and not rewritten.

Return values you must define

Specify exact results for these cases:

  • Missing accounts.
  • Insufficient funds.
  • source == target.
  • Unknown, expired, duplicate or wrong-account accept calls.
  • Invalid merges.

Failed calls must not change any state. Explain how a repeated accept stays safe.

Hints

Hint 1

Give every transfer a small state machine (PENDING, ACCEPTED, EXPIRED). Make `accept` succeed only from PENDING, and that status check also gives you idempotency.

Hint 2

Put `(createdAt + EXPIRY, transferId)` in a min-heap and run a sweep at the start of every public call. Skip entries that are no longer PENDING instead of deleting them from the middle.

Hint 3

Keep customer identity separate from account identity. Transfers and history refer to account ids, so a merge only changes an ownership pointer and never has to touch past records.