Skip to the page

Classic problems · mid

Splitwise, vending machine and ATM

These problems are about money rules and machine states. Keep money exact, validate inputs, check before taking money, and model the machine as a state machine with a safe refund path.

The notes

What you must know

  • Splitwise classes: User, Expense (payer, amount, splits, type), Split, a SplitStrategy per type (Equal, Exact, Percent) with validate and computeShares, an ExpenseService, and a balance sheet as a map of user to map of user to amount.
  • Validation: exact amounts must add up to the total, percentages to 100, reject unknown users, amount above 0. Rounding rule stated: for example the first person takes the extra paisa (33.34, 33.33, 33.33). Money in paise or BigDecimal.
  • A new split type is a new class plus one registry entry. To simplify debts, compute each net balance, then repeatedly settle the biggest debtor against the biggest creditor: at most n minus 1 payments. The true minimum is a hard problem, so greedy is the usual answer.
  • Vending machine: VendingMachine, Inventory, Product, Denomination, CashBox, PaymentMethod interface. State classes IdleState, HasMoneyState, DispensingState and OutOfServiceState; invalid actions give a clear error.
  • Change-making: greedy by denomination works for Indian denominations with unlimited coins, but with limited counts greedy can fail (6 rupees from one 5 and three 2 rupee coins). Search a little or refuse up front with exact change only.
  • Order of steps: check stock and change before taking the money, refund on any failure, and a failed dispense refunds and marks the slot faulty.
  • ATM: states Idle, CardInserted, Authenticated, TransactionInProgress and OutOfService. Check the amount is a multiple of 100 rupees and the note mix is possible before debiting. Debit then dispense with a transaction id; if dispensing fails, reverse with the same id (idempotent) and keep a journal.
  • The bank counts PIN attempts, not only the machine, and blocks the card after 3. RBI rule: a failed cash withdrawal must be reversed within T+5 days.

The red pen

Common traps: mistake → fix

  • Mistake: double is fine for balances

    Fix: it drifts by 0.01; use paise or BigDecimal.

  • Mistake: Store both A owes B and B owes A

    Fix: they can disagree; store one net value.

  • Mistake: Give the product before checking change

    Fix: then you cannot refund cleanly.

  • Mistake: Trust the machine PIN counter alone

    Fix: the bank must count.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design Splitwise. How do you support equal, exact and percent splits?
  2. How do you simplify debts?
  3. Design a vending machine. Why use the State pattern?
  4. Change-making with limited coins: when does greedy fail?
  5. Design an ATM. What if dispensing fails after debit?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview