KatInterview

In-Memory Bank Ledger with Pending Transfers and Expiry

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure DesignData StructuresSimulationDesign

Last updated

Simulate an in-memory banking system processing sequential operations, pending transfers with fixed-window expiration, and account activity tracking.

Design an in-memory ledger engine for a financial service. The system must process a list of sequential operations, maintain account balances, process delayed transfers with timed expirations, and rank accounts based on total activity.

You are given a parameter transfer_window, a list of operation names operations, and a matching list of parameter lists arguments.

Before executing any operation at a given timestamp, the engine must process all pending transfers that have expired. A transfer created at time $T_{create}$ with a window $W$ is active up to and including timestamp $T_{create} + W$. It expires as soon as an operation is processed at a timestamp $T_{current} > T_{create} + W$. When a transfer expires, its reserved funds are automatically refunded to the sender's account.

Each operation has a timestamp as its first parameter. Timestamps across all operations are guaranteed to be strictly increasing.

Supported Operations

  1. create_account(timestamp, account_id)

    • Creates an account named account_id with an initial balance of 0.
    • Returns True if created successfully.
    • Returns False if account_id already exists.
  2. deposit(timestamp, account_id, amount)

    • Adds amount to account_id's balance and increases account_id's total activity count by amount.
    • Returns the updated balance (an integer) on success.
    • Returns None if account_id does not exist.
  3. pay(timestamp, account_id, amount)

    • Deducts amount from account_id's balance and increases account_id's total activity count by amount.
    • Returns the updated balance (an integer) on success.
    • Returns None if account_id does not exist or if the available balance is less than amount.
  4. top_activity(timestamp, n)

    • Returns a list of up to n accounts with the highest cumulative activity, formatted as [[account_id, activity], ...].
    • Tie-breaking: If two accounts have the same activity score, order them lexicographically (alphabetically) by account_id.
    • If n is greater than the total number of existing accounts, return all existing accounts in ranked order.
  5. transfer(timestamp, source_id, target_id, amount)

    • Immediately deducts amount from source_id's balance to hold the funds.
    • Does not credit target_id immediately, and does not count towards activity scores.
    • Returns a transfer string ID of the form "transfer1", "transfer2", ..., incremented sequentially for every attempted transfer operation (whether successful or rejected).
    • Returns None (and does not change state or increment transfer ID counter) if source_id or target_id does not exist, if source_id == target_id, or if source_id has insufficient funds.
  6. accept_transfer(timestamp, account_id, transfer_id)

    • Finalizes a pending transfer by adding the held funds to account_id's balance.
    • Does not affect activity scores.
    • Returns True if successful.
    • Returns False if transfer_id does not exist, is not in a pending state, has already expired, or if account_id is not the target account of the transfer.

Constraints

  • Timestamps are strictly increasing integers: $1 \le \text{timestamp}i < \text{timestamp}{i+1}$.
  • Amounts are positive integers: $1 \le \text{amount} \le 10^9$.
  • $1 \le \text{transfer_window} \le 10^5$.
  • $1 \le N \le 1000$, where $N$ is the length of operations.
  • Returned account rankings for top_activity must format each pair as a 2-element list [account_id, activity].

Examples

Example 1

Input: run_bank_operations(5, ['create_account', 'create_account', 'deposit', 'transfer', 'accept_transfer', 'top_activity'], [[1, 'alice'], [2, 'bob'], [3, 'alice', 100], [4, 'alice', 'bob', 40], [5, 'bob', 'transfer1'], [6, 2]])

Output: [True, True, 100, 'transfer1', True, [['alice', 100], ['bob', 0]]]

Example 2

Input: run_bank_operations(3, ['create_account', 'create_account', 'deposit', 'transfer', 'pay', 'top_activity'], [[1, 'a'], [2, 'b'], [3, 'a', 50], [4, 'a', 'b', 30], [5, 'a', 25], [6, 5]])

Output: [True, True, 50, 'transfer1', None, [['a', 50], ['b', 0]]]

Hints

Hint 1

Make sure to update pending transfers before executing the operation at the current timestamp.

Hint 2

Expirations occur when current_timestamp > create_timestamp + transfer_window. Refund the held money to the sender when a transfer expires.

Hint 3

Activity only increases on successful deposit and pay operations. Transfers and acceptance of transfers do not affect activity.

Hint 4

For top_activity tie-breaking, sort primarily by activity descending, and secondarily by account_id ascending (lexicographical order).