KatInterview

Banking Ledger with Time-Based Transfers and Account Consolidation

Data Structure DesignAuto-graded · 13 test cases
HardAuto-gradedData Structure Designunion-findsimulationsorting

Last updated

Build an in-memory banking system that processes timed fund transfers deterministically and consolidates accounts with automatic transaction rerouting.

Problem Statement

Build an in-memory banking ledger that handles accounts, delayed fund transfers, and account consolidation. All monetary amounts are non-negative integers representing cents.

Implement a BankingLedger class with these operations:

Operations

create_account(account_id: str, initial_balance: int) -> bool

Opens a new account with the given identifier and starting balance. Returns False if the account identifier is already in use or if the initial balance is negative. Returns True on success.

schedule_payment(payment_id: str, timestamp: int, from_account: str, to_account: str, amount: int) -> bool

Registers a future transfer to be executed when time advances to the specified timestamp. Each payment identifier must be unique across all time—once used, a payment ID cannot be reused even if the payment failed or was cancelled.

Returns False if:

  • The payment identifier has been used before
  • Either account does not exist
  • Both accounts resolve to the same active account
  • The amount is zero or negative

Returns True when the payment is successfully registered.

cancel_payment(payment_id: str) -> bool

Cancels a pending payment before it executes. Returns True if the payment was pending and is now cancelled. Returns False if the payment does not exist, has already executed, has already failed, or was already cancelled.

merge_accounts(source_account: str, target_account: str) -> bool

Consolidates the source account into the target account. The source account's entire balance is transferred to the target, and the source account is permanently retired. All future operations involving the retired account will automatically use the target account instead.

If multiple consolidations form a chain (A consolidated into B, then B into C), all references to A or B automatically resolve to C.

Returns False if:

  • Either account does not exist
  • Both accounts resolve to the same active account
  • Source and target are identical

Returns True on success.

advance_time(current_time: int) -> List[dict]

Executes all pending payments with timestamp ≤ current_time in deterministic order:

  1. Payments with earlier timestamps execute first
  2. Among payments with the same timestamp, execute in lexicographic order by payment_id (ascending)

For each payment, the system resolves both accounts to their current active accounts, then attempts the transfer:

  • If the source account has sufficient balance, the transfer succeeds (status "SUCCESS")
  • If the source account has insufficient balance, the payment fails (status "FAILED") and no money moves

Returns a list of dictionaries in execution order: {"payment_id": str, "status": "SUCCESS" | "FAILED", "timestamp": int} where timestamp is the payment's scheduled time.

get_balance(account_id: str) -> int

Returns the current balance of the account. If the account was consolidated into another account, returns the balance of the active target account. Returns -1 if the account identifier was never created.

Constraints

  • Account identifiers and payment identifiers are non-empty strings
  • All balances and amounts are non-negative integers (0 ≤ value ≤ 10^9)
  • Timestamps are integers (may be negative, zero, or positive)
  • At most 10^4 total operations across all method calls
  • Account identifiers are at most 20 characters
  • Payment identifiers are at most 30 characters

Examples

Example 1

Input: execute_banking_operations(['create_account', 'create_account', 'create_account', 'schedule_payment', 'schedule_payment', 'advance_time', 'merge_accounts', 'get_balance', 'get_balance'], [['acc1', 100], ['acc2', 50], ['acc3', 0], ['p2', 10, 'acc1', 'acc2', 60], ['p1', 10, 'acc1', 'acc3', 50], [10], ['acc3', 'acc2'], ['acc2'], ['acc3']])

Output: [True, True, True, True, True, [{'payment_id': 'p1', 'status': 'SUCCESS', 'timestamp': 10}, {'payment_id': 'p2', 'status': 'FAILED', 'timestamp': 10}], True, 100, 100]

Hints

Hint 1

Use union-find to track account consolidation chains. Each account points to its parent, and roots represent active accounts with meaningful balances.

Hint 2

Store original account identifiers in payment records and resolve them to active accounts only when executing. This automatically handles rerouting through consolidation chains.

Hint 3

When multiple payments have the same timestamp, sort by payment_id to ensure deterministic execution order. Earlier payments in the batch affect the balances available for later payments.