KatInterview

Simulate a Time-Aware Financial Ledger

Data Structure DesignAuto-graded · 15 test cases
HardAuto-gradedData Structure Designdesign-dsheaps-sortingarrays

Last updated

Process sequential financial operations with scheduled transfers, account merging, historical snapshot queries, and activity rankings using heaps and binary search.

Design an engine to process a sequence of chronological financial requests for a time-aware banking network. The function process_bank_operations(operations, arguments) accepts two list parameters: operations, containing command strings, and arguments, containing lists of positional arguments for each command.

Every command's first positional argument is an integer timestamp. Across the sequence of operations, these timestamps are non-decreasing.

Automatic Processing: Pending Scheduled Transfers

Before carrying out any command at time timestamp, all scheduled transfers due at or before timestamp (scheduled_time <= timestamp) must execute in chronological order. If multiple transfers share the exact same scheduled execution time, break ties by ordering transfer_id lexicographically ascending.

During scheduled execution:

  • Re-check that both sender and recipient are active, sender $\neq$ recipient, and sender balance $\ge \text{amount}$.
  • If any check fails, the transfer is dropped silently.
  • On success, transfer the funds, update cumulative outbound totals, and log balance changes for both accounts at scheduled_time (not the current operation timestamp).

Command Specifications

  • create_account(timestamp, account_id)

    • Creates a new account account_id (string) with balance 0.
    • Returns True if successful, or False if account_id is currently active.
    • Note: If an account was previously merged away (deactivated), re-creating it succeeds and resets its state, balance, and spending history.
  • deposit(timestamp, account_id, amount)

    • Adds amount (integer $> 0$) to the active account account_id.
    • Logs the new balance at timestamp.
    • Returns the updated balance as an integer, or None if account_id is not active.
  • transfer(timestamp, from_id, to_id, amount)

    • Instantly transfers amount from from_id to to_id.
    • Fails if from_id or to_id is not active, from_id == to_id, or balances[from_id] < amount.
    • On success, updates balances, logs balance history for both accounts at timestamp, adds amount to from_id's total outbound transfer volume, and returns True. Otherwise returns False.
  • schedule_transfer(timestamp, scheduled_time, transfer_id, from_id, to_id, amount)

    • Registers a future transfer with unique identifier transfer_id (string).
    • Returns False if scheduled_time <= timestamp, if transfer_id has ever been used before (including previously executed or cancelled transfers), if from_id or to_id is not active, or if from_id == to_id.
    • Balance sufficiency is not checked at registration time.
    • Returns True on successful scheduling.
  • cancel_transfer(timestamp, transfer_id)

    • Cancels a pending scheduled transfer.
    • Returns True if successfully cancelled, or False if transfer_id is not currently pending (e.g., non-existent, already executed, or previously cancelled).
  • merge_accounts(timestamp, target_id, source_id)

    • Merges active source_id into active target_id. Fails (returns False) if either account is inactive or target_id == source_id.
    • Transfers source_id's remaining balance to target_id. (This balance movement does not count as outbound transfer volume).
    • Adds source_id's cumulative outbound volume to target_id's outbound volume.
    • Rebinds all currently pending transfers involving source_id so they reference target_id instead.
    • Deactivates source_id (setting its active balance to 0 and logging snapshots at timestamp for both accounts).
    • Returns True on success.
  • get_balance(timestamp, account_id, as_of_time)

    • Retrieves account balance as of as_of_time (as_of_time <= timestamp).
    • Returns None if account_id was never created or if as_of_time is strictly prior to the account's initial creation time.
    • Returns the account's balance after processing all events up to and including as_of_time. This query works for both active and deactivated accounts.
  • get_top_spenders(timestamp, n)

    • Returns a list of strings containing up to the top n active accounts ranked by cumulative outbound transfer volume descending.
    • Break ties lexicographically by account_id ascending.
    • Deactivated accounts are omitted. Outbound volume counts direct and scheduled transfers only (deposits and balance shifts from mergers are excluded).

Constraints

  • $1 \le \text{timestamp}, \text{scheduled_time} \le 10^9$
  • Total number of operations $\le 20,000$
  • amount $> 0$

Examples

Example 1

Input: process_bank_operations(['create_account', 'create_account', 'deposit', 'schedule_transfer', 'get_balance', 'get_balance', 'get_top_spenders'], [[1, 'A'], [1, 'B'], [2, 'A', 100], [3, 10, 't1', 'A', 'B', 40], [5, 'A', 4], [11, 'A', 10], [12, 1]])

Output: [True, True, 100, True, 100, 60, ['A']]

Hints

Hint 1

Process due scheduled transfers before every operation call by pulling from a min-heap sorted by (scheduled_time, transfer_id).

Hint 2

Use lazy deletion for heap entries: store the active scheduled parameters in a map, and remove the entry when cancelled or updated.

Hint 3

When merging accounts, rewrite the sender and receiver IDs in the pending transfer lookup dictionary while keeping the heap unchanged.