KatInterview

Model a Prepaid Spending Account with Transaction Limits

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented designconcurrencystate management

Last updated

Design a class hierarchy that tracks a spending cap, running balance, and transaction history while safely handling concurrent charges and edge-case inputs.

Prepaid Spending Account

You are building the core logic for a prepaid account system. A user loads a fixed amount of money onto the account and can make purchases against that balance until it runs out.

What to build

SpendingAccount — the main class. It holds:

  • the account holder's name (optional)
  • a spending cap (the maximum total that can ever be charged)
  • the current amount already spent

Charge — a record representing one attempted purchase. It stores:

  • the charge amount
  • when the attempt was made (UTC timestamp)
  • a unique ID
  • the billing period it belongs to
  • its outcome: PENDING, APPROVED, or DECLINED

ChargeLog — a separate, append-only store for Charge records. It has no knowledge of caps or balances; it just stores and retrieves entries.

Methods to implement

attempt_charge(amount) -> bool Try to spend amount from the account. Return True if the charge goes through, False otherwise. A charge is approved only when it does not push the total spent past the cap. The check and the balance update must be atomic — assume the account can receive concurrent requests.

current_balance() -> Decimal Return the total amount charged so far.

available_funds() -> Decimal Return how much the account can still absorb.

get_period_statement() -> list[Charge] Return all approved charges in the current billing period. Declined attempts and charges from earlier periods must not appear.

close_period() -> None Advance to the next billing period. The running balance carries over (this is not a payment).

Constraints

  • A charge must be declined if it would cause the total spent to exceed the cap — even by a fraction.
  • A charge of exactly the remaining available funds must be approved.
  • Zero, negative, non-numeric, and infinite values must be declined.
  • Thread safety is required: two concurrent charges that together exceed the cap must not both be approved.
  • Keep account state and charge history in separate objects.

Example

account = SpendingAccount(cap=1000)
account.attempt_charge(500)   # True
account.attempt_charge(600)   # False — would exceed cap
account.current_balance()     # Decimal('500')
account.get_period_statement()
# [Charge(500, Approved)]

Edge cases to consider

  • Amount is zero or negative
  • Amount exactly equals the remaining available funds
  • Two threads each try to charge 600 on a 1000-cap account with 500 already spent
  • The charge log is empty
  • get_period_statement() is called after close_period()

Hints

Hint 1

Start by identifying the single invariant the system must protect: `0 <= balance <= cap`. Then ask yourself — where is the one place that invariant can break, and how do you guard it?

Hint 2

The race condition lives in the read-compare-write sequence on the balance. Locking only the write step is a classic bug — the check and the update must share the same critical section.

Hint 3

Use Python's `Decimal` type (constructed from a string, not a float) to store amounts. Binary floats make `0.1 + 0.2 != 0.3`, which can cause a charge at the exact cap to be wrongly declined. In production, store integer cents instead.