KatInterview

Mini Ledger Service: Credits, Debits and Safe Balances

System DesignModel answer
MediumModel answerSystem Designapi-designtransactionsidempotency

Last updated

Design and build a small full-stack ledger app with an API, storage, a basic UI and tests that records credits and debits and never lets an account balance drop below zero.

Mini Ledger Service

You are asked to build a small web application, using any AI coding assistant you like, that tracks money moving in and out of accounts. The goal is a working, well-reasoned vertical slice rather than a polished product.

What the app must do

  • Offer an HTTP API (REST or GraphQL) to record a credit (adds funds) or a debit (removes funds) against an account and to read the account's current balance.
  • Store data durably. An in-memory store or a lightweight embedded database is acceptable.
  • Provide a simple frontend where a user can submit a transaction and see the account balance and its transaction history.
  • Validate every input on the server (transaction type, amount, account existence, field lengths).
  • Guarantee that an account balance never goes negative, including when several requests arrive at the same time.
  • Include a basic automated test suite.
  • Supply setup instructions and a short README.

What to cover in your answer

Describe how you would approach this as a time-boxed exercise (about an hour). Your answer should address:

  1. The assumptions and scope you would state up front.
  2. Technology choices and why.
  3. API endpoints, request/response shapes and error codes.
  4. The data model and how you represent money.
  5. How you prevent overdrafts under concurrent requests, and how you make client retries safe.
  6. What the frontend needs to contain.
  7. Which tests you would write.
  8. How you would supervise the AI tool and verify what it produces.
  9. What you would change to take this to production.

Hints

Hint 1

Decide how money is stored before anything else: think about what goes wrong with floating-point arithmetic, and pick a representation that is exact.

Hint 2

A check-then-write sequence (read the balance, compare in application code, then write) is racy. Look for a way to make the check and the update one atomic operation inside a database transaction, and back it with a database constraint.

Hint 3

Clients retry on timeouts. Consider a client-supplied idempotency key with a uniqueness constraint so a repeated debit is applied only once, and write a concurrency test that fires many parallel debits at one account.

Hint 4

When using an AI tool, review its output specifically for float amounts, missing validation, unchecked balance logic and absent tests, then prompt it to fix each issue.