KatInterview

In-Memory Bank With Delayed Payments, Cancellation and Spend Ranking

Data Structure DesignModel answer
HardModel answerData Structure Designdesign-dsheaplazy-deletion

Last updated

Extend an in-memory bank with delayed payments that can be canceled, record success or failure for each, discard stale entries, and rank accounts by total outgoing money.

Delayed Payments in an In-Memory Bank

You are building a single-process, in-memory banking class. Every public method takes a timestamp as its first argument, and timestamps strictly increase from one call to the next. Accounts already support creation, deposits and transfers between accounts. Your job is to add scheduled payments.

Requirements

  1. Scheduling. schedule_payment(timestamp, account_id, amount, delay) queues a withdrawal that executes at timestamp + delay.
    • It returns a unique identifier ("payment1", "payment2", ...), or None if the account does not exist.
    • Persist the result of every payment, whether it succeeded or failed, together with a reason for failure and the time it was processed.
  2. Execution order. At any given timestamp, all payments that have come due are executed before the current operation does its own work. Payments due at the same moment run in the order they were created.
    • A payment succeeds if the balance covers it. Otherwise it fails and nothing changes (no balance change, no effect on outgoing totals).
  3. Cancellation. cancel_payment(timestamp, account_id, payment_id) returns True only if the payment exists, belongs to that account, and has not yet been executed or canceled. Otherwise it returns False.
  4. Cleanup. Canceled payments must not run later, and they should not accumulate in your pending-work structure indefinitely. Results of executed payments must remain queryable.
  5. Outgoing report. top_spenders(timestamp, n) returns up to n entries formatted as "account_id(total)", where total is the money that has left the account (outgoing transfers plus successful payments).
    • Sort by total descending, then by account id ascending.
    • Accounts with no outgoing money still count with a total of 0.

What to deliver

Describe your data model and assumptions, explain how you handle due payments, cancellation and cleanup, give the code, and state the time complexity of each operation. Mention how you would test it and how the design might change if it had to scale beyond one process.

Hints

Hint 1

Keep two separate things: a persistent record of every payment with its status, and a work queue holding only what still needs to run. Which structure keeps the queue ordered by due time and creation order?

Hint 2

Removing an arbitrary item from a heap is expensive. Consider just marking a payment as canceled and skipping it when it reaches the top of the queue, then rebuilding the queue when too many dead entries pile up.

Hint 3

Call the same 'process everything due up to this timestamp' routine at the start of every public method, and again after enqueueing a payment with delay 0, so the ordering rules hold without background threads.