KatInterview

Design an In-Memory Ledger with Transfers and Activity Ranking

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designhash-mapheap

Last updated

Build an in-memory account ledger in stages—create/deposit, transfer, top-N by activity—plus a command-string test harness with clear failure semantics.

Context

You are asked to build an in-memory banking ledger as a class, incrementally, in a single interview slot. The work comes in four stages: basic accounts and deposits, transfers, a "most active accounts" ranking, and finally a small harness that runs table-driven test cases made of string commands against a fresh instance of your class.

This is a whiteboard/verbal round: you write code (or pseudocode with real signatures) and talk through the design. Explain your reasoning as you go, state assumptions out loud, and call out complexity. The harness is expected to be a real deliverable, not an afterthought—budget roughly a third of the time for it.

Ground rules

  • Everything lives in memory. No persistence, no networking, single-threaded.
  • Account IDs are non-empty strings; you may reject empty or duplicate IDs.
  • Amounts are integers in the smallest currency unit. Do not use floating point for money.
  • Command names and output strings are whatever the test data expects; the examples here are illustrative—confirm the exact format with your interviewer before hardcoding.

Clarify before coding

  • What does each operation return on success, and how are failures reported (exception vs. error value vs. sentinel string)?
  • Are zero or negative amounts rejected, and is that per operation or a general rule?
  • Is transferring from an account to itself legal? Is it a no-op or an error?
  • For the ranking: is "activity" the count of operations, the summed amount moved, or something else? Do both the sender and receiver of a transfer earn credit? Do failed operations or account creation count?

Part 1 — Accounts and deposits

Implement create_account(account_id) and deposit(account_id, amount).

  • Creating an account that already exists must fail; on success the balance is 0.
  • Depositing to a missing account must fail; on success return the new balance.
  • Both operations should be O(1) average.

Part 2 — Transfers

Implement transfer(source_id, target_id, amount), which moves money between two existing accounts and returns the source's new balance.

  • Fail if either account is missing, if the amount is invalid, or if the source has insufficient funds.
  • A failure must leave both balances untouched (validate everything before mutating).
  • Decide and justify the self-transfer behavior.
  • Extend the Part 1 design rather than rewriting it.

Part 3 — Top activity

Implement top_activity(n), returning the n most active accounts, most active first, as a list of (account_id, activity) pairs or formatted strings.

  • The definition of activity must be confirmed with the interviewer and then maintained incrementally (not recomputed from a log).
  • Ties must resolve deterministically—state the rule.
  • If n exceeds the number of accounts, return all of them.

Part 4 — Test harness

Write a runner that takes a test (a list of operations, each a string array whose first element is the command name), creates a fresh ledger instance, dispatches each operation, and produces one output string per operation in order. Example input:

[["CREATE_ACCOUNT","acc1"],
 ["CREATE_ACCOUNT","acc2"],
 ["DEPOSIT","acc1","500"],
 ["TRANSFER","acc1","acc2","200"],
 ["TOP_ACTIVITY","2"]]

Dispatch must be easy to extend with new commands, output formatting must be consistent (including failures), and when actual output differs from expected output you should report which operation index and which command failed.

Hints

Hint 1

Pin down the meaning of 'activity' and the return/failure format before writing Part 3 and Part 4 code—these choices ripple back into every method signature.

Hint 2

Keep an accounts map from ID to a small record (balance + activity counter). For ranking, either sort a snapshot or use a bounded min-heap when n is much smaller than the number of accounts; a full sort is usually fine for interview scale.

Hint 3

In the harness, use a table mapping command name to a handler, parse integers in one place, and wrap each call so an exception becomes an error string instead of crashing the run.