ZipRecruiter logoZipRecruiter
CodingFree preview

Banking System

Implement an in-memory banking system that processes timestamped operations. The spec grows in progressive levels from basic account operations to ranking, scheduled payments, account merging and historical balance queries.

SWE
hard
hashmap
heap
simulation
union-find
design
Frequency
Low
Last asked
2026-10-08
Stage
oa

Problem

Build an in-memory BankingSystem over a stream of timestamped operations; timestamps are strictly increasing across all calls (1 to 10^9). The spec arrives in four progressive levels on the same class.

  1. create_account(ts, id) -> bool (False if it exists); deposit(ts, id, amount) -> int | None (new balance, None if no account); transfer(ts, source, target, amount) -> int | None (source's new balance; None if an account is missing, source equals target, or funds are insufficient).
  2. top_spenders(ts, n) -> list[str]: rank by total outgoing money (transfers out plus executed scheduled payments), descending, ties by account_id ascending, formatted "<id> (<outgoing>)"; zero-outgoing accounts count; return all if fewer than n.
  3. schedule_payment(ts, id, amount, delay) -> str | None returns payment<N> (N global across accounts) and runs at ts + delay; it is silently skipped if funds are short. cancel_payment(ts, id, payment_id) -> bool is False for an unknown, already canceled or executed payment, or a wrong owner. Payments due at t run before any other operation at t, in creation order; a cancel at t comes after them. Only executed payments count as outgoing.
  4. merge_accounts(ts, id1, id2) -> bool folds account 2 into 1 (balances add, pending payments move to account 1, account 2 is removed, outgoing totals combine); False if the ids are equal or either is missing. get_balance(ts, id, time_at) -> int | None returns the balance as of time_at, including operations at that exact time, or None if the account did not exist then; a merged account inherits the merged-away account's history.

Variants

  • The same four-level OA is also taken in C++. In that sitting the levels were summarized as: create accounts, deposit, transfer, and merge one account into another (removing the merged-away account).
    • Minority variant: the transfer level was described as carrying a latency / delayed-settlement aspect. Clarify before coding whether a transfer settles immediately or only after a delay, since that decides whether transfers share the scheduled-payment queue.

Hints

Pitfalls

  • A payment due at the same timestamp as another operation must run first; draining at the top of every method enforces it.
  • Cancelling a payment that executed at the same timestamp returns False; a skipped payment is silent and adds nothing to outgoing.
  • transfer with source equal to target returns None; after a merge, any call naming the merged-away id behaves as if the account does not exist.
  • get_balance for a time before the account was created returns None, not 0.
  • None of the levels is individually hard, but the four descriptions are long. Read each level's rules carefully and budget the clock so Level 4 is not starved.

Talking Points

  • Design for Level 4 from the start: tracking only current balances forces a data-model rewrite when get_balance appears.

Follow-ups

Preparation

  • Implement the event log and test get_balance against a brute-force replay.
  • Write the lazy heap drain with cancellation and test same-timestamp ordering.
  • Implement merge via parent pointers and cancel a merged-away account's payment through the surviving id.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...