KatInterview

Design a Money Transfer System with Pending Approvals

Data Structure DesignModel answer
MediumModel answerData Structure Designconcurrencyidempotencystate-machine

Last updated

Build an in-memory banking service supporting accounts, transaction history, and two-party transfers that only settle when the recipient explicitly accepts.

Money Transfer Service with Recipient Approval

You are building a lightweight in-memory financial service. The system needs to manage accounts, record every mutation, and handle transfers that do not settle until the receiving party confirms them.

Requirements

1. Accounts and basic operations

  • create_account(account_id, initial_balance) — registers a new account with a unique ID and a starting balance (in integer cents; no floats). Reject duplicate IDs and negative balances.
  • deposit(account_id, amount) — adds funds; amount must be a positive integer.
  • withdraw(account_id, amount) — subtracts funds; reject if the resulting balance would go below zero.

2. Transaction history

Every successful mutation appends an immutable record containing:

  • a monotonically increasing sequence number
  • a wall-clock timestamp
  • an operation label: deposit, withdrawal, transfer_pending, transfer_in, transfer_out
  • the amount involved
  • the balance immediately after the operation
  • an optional reference to a related transfer ID

Provide:

  • get_history(account_id) — returns the full ordered list of records for an account
  • get_recent(account_id, n) — returns only the last n records

3. Two-party transfers

  • create_transfer(transfer_id, from_id, to_id, amount) — initiates a transfer in PENDING state. No balances change at this point. A transfer_pending entry is appended to the sender's history. Transfers expire after a configurable TTL (default 24 hours). Support an optional idempotency_key so retried requests do not create duplicate transfers.
  • accept_transfer(transfer_id, account_id) — only the intended recipient may call this. Re-validates that the sender has sufficient funds at acceptance time. Moves the transfer to ACCEPTED, debits the sender, and credits the recipient atomically. Appends transfer_out to the sender and transfer_in to the recipient.
  • reject_transfer(transfer_id, account_id) — only the recipient may call this. Moves the transfer to REJECTED. No balances change.

State rules:

  • Valid transitions: PENDING → ACCEPTED, PENDING → REJECTED, PENDING → EXPIRED
  • Calling accept or reject on an already-settled transfer is a no-op (return the current status), not an error — unless the caller conflicts with the recorded final state in a way that would be nonsensical.
  • An expired transfer cannot be accepted or rejected.

What to deliver

Implement the service in the language of your choice. Then:

  • State the data structures you chose and why.
  • Give the time and space complexity for each operation.
  • Discuss at least four edge cases and how you handle them.
  • Explain how you prevent deadlocks and ensure exactly-once settlement.

Constraints

  • All monetary values are integers (cents). Reject floats or negative amounts.
  • No external databases or queues — everything lives in memory.
  • The solution must be safe under concurrent calls.

Hints

Hint 1

Funds may be spent between when a transfer is created and when it is accepted. Where exactly should the balance check happen — and under which lock?

Hint 2

To avoid deadlock when locking two accounts at once, establish a consistent lock ordering before you write a single line of concurrent code.

Hint 3

A per-transfer lock plus a sorted two-account lock covers acceptance. But what ordering ensures the transfer lock is always acquired before the account locks, never after?