Circle logoCircle
CodingFree preview

Banking System (CodeSignal OA)

Four-level in-memory bank: (1) create/deposit/transfer; (2) top-k spenders by outgoing amount, formatted `id(total)`; (3) scheduled payments with cancel, processed before every operation, with ties broken by creation order; (4) merge accounts (balance, spending, and pending payments) plus a balance lookup. One rotation swaps Level 3 for two-phase transfers that must be accepted before they expire.

SWE
medium
codesignal
object-design
hashmap
heap
top-k
sorting
Frequency
Medium
Last asked
2026-09-24
Stage
oa

Problem

CodeSignal OA, four progressively unlocked levels. Implement an in-memory bank. Most methods take the current timestamp as the first argument, and operations arrive in non-decreasing timestamp order.

Level 1 — Basic operations

  • createAccount(accountId) -> bool: create the account. Return True if it didn't exist, False if it already exists.
  • deposit(timestamp, accountId, amount) -> int | None: add amount and return the new balance. Return None if the account doesn't exist.
  • transfer(timestamp, fromId, toId, amount) -> int | None: both accounts must exist, fromId != toId, and the source must have enough balance. On success, debit fromId, credit toId, and return fromId's new balance. Otherwise return None.

Level 2 — Top spenders

  • topSpenders(timestamp, k) -> list[str]: the k accounts with the highest total outgoing amount, sorted by amount descending and then by accountId ascending.
  • Outgoing includes transfers sent and executed scheduled payments. It does not include deposits or money received.
  • The return format is usually "accountId(total)", e.g. ["alice(1200)", "bob(900)", "charlie(900)"]. A bare id list ["alice", "bob"] also appears. Check the expected output before coding.

Level 3 — Scheduled payments

  • schedulePayment(timestamp, accountId, amount, delay) -> str | None: if the account exists, create a pending payment that executes at timestamp + delay and return a unique id: "payment1", "payment2", …. Return None if the account doesn't exist.
  • cancelPayment(timestamp, accountId, paymentId) -> bool: succeeds only if the payment exists, belongs to accountId, and hasn't executed yet.
  • Before handling any operation, first process every pending payment with execution time <= timestamp.
  • When a payment executes, it debits the account and counts toward spending if the balance is sufficient. Otherwise it fails silently: no debit, no spending. Either way it is consumed and never retried.
  • Payments that share an execution time run in creation order.

Level 4 — Merge accounts and balance lookup

  • mergeAccounts(timestamp, targetId, sourceId) -> bool: both accounts must exist and targetId != sourceId. Add the source's balance and its total spending to the target, move all of the source's pending payments to the target, then delete the source.
  • getBalance(timestamp, accountId) -> int | None: the current balance, or None if the account doesn't exist. Due payments are processed first, as with every other timestamped call.
class Bank:
    def createAccount(self, accountId: str) -> bool: ...
    def deposit(self, timestamp: int, accountId: str, amount: int) -> int | None: ...
    def transfer(self, timestamp: int, fromId: str, toId: str, amount: int) -> int | None: ...
    def topSpenders(self, timestamp: int, k: int) -> list[str]: ...
    def schedulePayment(self, timestamp: int, accountId: str, amount: int, delay: int) -> str | None: ...
    def cancelPayment(self, timestamp: int, accountId: str, paymentId: str) -> bool: ...
    def mergeAccounts(self, timestamp: int, targetId: str, sourceId: str) -> bool: ...
    def getBalance(self, timestamp: int, accountId: str) -> int | None: ...

Examples

createAccount("A") -> True
createAccount("B") -> True
deposit("A", 1000) -> 1000
deposit("B", 300) -> 300
transfer("A", "B", 200) -> 800          # A spending +200
topSpenders(2) -> ["A(200)", "B(0)"]
schedulePayment(10, "B", 100, 5) -> "payment1"   # would execute at 15
cancelPayment(12, "B", "payment1") -> True
schedulePayment(20, "A", 300, 5) -> "payment2"   # executes at 25
getBalance(24, "A") -> 800              # payment2 not due yet
getBalance(26, "A") -> 500              # payment2 executed, A spending +300
topSpenders(2) -> ["A(500)", "B(0)"]
mergeAccounts(30, "A", "B") -> True
getBalance(30, "A") -> 1000             # 500 + B's 500
getBalance(30, "B") -> None

Variants

Level 1 with pay and Level 3 with pending transfers

In one version of the rotation, Level 1 is create account / deposit / pay, and Level 2 ranks the top n accounts using statistics over all deposit and pay operations. Level 3 replaces scheduled payments with two-phase transfers: the sender's funds are held when the transfer is created, and the recipient is credited only after the transfer is accepted. Acceptance has a time limit. If it expires, the held amount goes back to the sender.

  • Minority variant: Level 4's second part may ask for an account's balance at a given point in time rather than its current balance. That means keeping per-account balance history. Clarify before coding.

Pitfalls

  • Process due payments before the current operation. After schedulePayment(1, "A", 100, 5) (due at 6), a later deposit(10, "A", 50) must first run the 100 debit at time 10, then apply the deposit.
  • Only outflows count as spending. Deposits and incoming transfers never do.
  • A failed scheduled payment (insufficient funds) must not debit the account or add to spending, and it can't run again later.
  • After a merge, the source account must be gone everywhere: balance lookups return None, transfers fail, and its pending payments now belong to the target. Merge the spending totals as well as the balances.
  • Merges also run due payments first.
  • Time is the real constraint. Candidates who code steadily through Levels 1–3 still run out of time on Level 4. One candidate fixed a Level 3 bug in a few minutes, had four minutes left for Level 4's second part, and scored 775. Several candidates finished only through Level 3.

Preparation

  • Build all four levels in one sitting under a 90-minute timer, in the language you type fastest.
  • Write the "process due payments up to timestamp" helper first, and call it at the top of every method.
  • Replay the example sequence above as unit tests, then add cases for cancelled payments, failed payments, and merging accounts that still have pending payments.
  • Separately, implement the two-phase transfer variant: hold, accept within a window, refund on expiry.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...