KatInterview

Layered Ledger Service: Cash, Transfers, Fees and Held Funds

Data Structure DesignModel answer
MediumModel answerData Structure Designledgermoney-handlingobject-oriented-design

Last updated

Design and code a bank ledger that grows in stages: cash operations, per-account statements with failures, internal and external transfers with fees, and delayed transfers that lock funds.

You are asked to build a small in-memory banking engine in five incremental stages. Each stage adds one feature, and the earlier stages must keep working without being rewritten. Only the core service class is judged. Any code that reads action files or prints statements is just scaffolding, and you may hardcode paths there.

You choose the method signatures and the input line format. Assume each input line is one timestamped action: time, kind, the account(s) involved, and an amount.

Ground rules

  • Money is in dollars and cents. Print it as $X.XX.
  • Times print with a non-padded hour, for example 2020-08-01 7:00:00.
  • State your assumptions explicitly where the spec is silent. Examples: whether input is time-sorted, whether accounts are created on first use, whether a balance may go negative, and how invalid amounts are treated.

Stage 1: Cash at an ATM

Support checking a balance, depositing cash and withdrawing cash. After processing all actions, print one line per account:

account amount
PEACH1111 $5.00
PEACH2222 $10.00

Decide and document what happens when a withdrawal exceeds the balance.

Stage 2: Per-account statements, failures included

Record every attempted transaction, including failed ones. Each row has: time, account, credit, debit, status, type, balance after.

time account credit debit status type balance
2020-08-01 5:00:00 PEACH2222 $20.00 SUCCESS ATM $20.00
2020-08-01 6:00:00 PEACH2222 $25.00 FAILED ATM $20.00

A failed withdrawal leaves the balance unchanged. Rows are grouped by account and ordered by time, with a deterministic tie-break for equal timestamps.

Stage 3: Instant transfers

Add instant_transfer(...), which moves money between two accounts. Both accounts show the transfer on their statements (a debit for the sender, a credit for the receiver). From here on, statements use a single amount column:

time account amount status type balance
2020-08-01 6:00:00 PEACH1111 $2.00 SUCCESS INSTANT TRANSFER $7.00

Define behavior for an unknown destination and for source equal to destination.

Stage 4: External-bank transfers with a fee

Support transfers to and from an account at another bank. Each external transfer also charges a fee, which appears as its own FEE row that debits the account:

2020-08-01 7:00:00 PEACH2222 $5.00 SUCCESS INSTANT TRANSFER $13.00
2020-08-01 7:00:00 PEACH2222 $0.01 SUCCESS FEE $12.99

State your fee rule (flat or percentage, rounding), whether it applies to both directions, and whether the funds check includes the fee.

Stage 5: Slow transfers

Add a transfer type where, once initiated, the money is unavailable to the sender until it arrives at the destination:

2020-08-01 6:00:00 PEACH1111 $2.00 SUCCESS SLOW TRANSFER $7.00

State how arrival time is determined, whether in-transit money counts toward the balance, and what happens if a transfer cannot complete.

What to deliver

Working code for as many stages as you can, with output that matches the formats above, plus a short explanation of your design, your assumptions and what you would do with more time. Reaching at least Stage 3 or 4 is a reasonable target.

Hints

Hint 1

Store money as integer cents (or Decimal) and never use floats. Format only at the output boundary.

Hint 2

Make an append-only list of transaction records the single source of truth. A record carries time, account, signed amount, status, type and balance after. Statements are views over it, and all balance changes go through one private debit/credit routine.

Hint 3

For transfers, validate everything first (accounts, amount, funds including any fee) and only then apply both legs, so a failure leaves no partial state. Put the fee rule in its own function. For slow transfers, keep a pending queue (a min-heap by arrival time) and settle everything due before processing each new action.