WHILELOOPWARRIOR(1)Design Interview ManualWHILELOOPWARRIOR(1)

~/lld/splitwise

Splitwise · 9005

A medium low-level design interview question on strategy, validation, money handling, greedy. 8 scenario tests.

SYNOPSIS

interviewer: Design an expense-sharing app.

Friends record who paid for what; the app tracks who owes whom and suggests how to settle up. About 40 minutes.

QUESTIONS TO ASK

Interviews start vague on purpose. Strong candidates ask before they draw; in the studio the interviewer answers these and the answers become your requirements.

  • How can an expense be split?
  • What does a balance mean?
  • How do we handle amounts that don't divide evenly?
  • What does settling up do?
  • What input is invalid?
  • Does the payer have to be part of the split?
  • Do we need groups?
  • Multiple currencies?

DESCRIPTION

The full statement, as the studio shows it at the implement step.

Design the core of an expense-sharing app: friends record who paid for what, the app keeps everyone's balance and suggests how to settle up.

requirements

  • addUser(id, name) registers a user.
  • addExpense({ paidBy, amount, split, between, shares }) records an expense:
split "equal"   - amount divided evenly between the users in `between`
split "exact"   - shares[i] is what between[i] owes, in currency
split "percent" - shares[i] is between[i]'s percentage of the amount

The payer may or may not be one of the people in `between`.

  • balance(id): positive means the others owe this user money, negative means the user owes. balances() returns { id: balance } for every user, in the order they were added.
  • settleUp() returns a list of transfers { from, to, amount } that brings every balance to zero. It does not change the balances.

money rules (all amounts have at most 2 decimals; return numbers, not strings)

  • Work in whole cents. When a split does not divide evenly, round each share down to a cent and give the leftover cents, one each, to the first people in `between`. Example: 10.00 between A, B, C is 3.34, 3.33, 3.33.
  • Percent shares are computed the same way: round down, leftover cents go to the first people in `between`.

settling up

Repeat until everyone is at zero: the user who owes the most pays the user who is owed the most, as much as possible. Ties go to the user who was added first.

errors (message must contain the quoted text)

  • a paidBy or between id that was never added: "unknown user"
  • amount <= 0: "amount"
  • exact shares that do not add up to the amount: "shares"
  • percent shares that do not add up to 100: "percent"

think about

  • Why floating point dollars break this, and what changes for currencies with 0 or 3 decimals.
  • Greedy settling is not always the minimum number of transfers. When?

GRADING

The studio runs the interview in seven steps (clarify, entities, class diagram, patterns, sequence, implement, review) and a coach grades each one live: the core objects you found, the relations between them (ownership vs. use, the right end of the diamond), responsibilities, whether your patterns really have the shape of the pattern, and a sequence diagram of the main use case. Then you implement it in JavaScript and run its 8 scenario tests in your browser. The review gives a score out of 100, a hire signal and a reference design to compare.

FOLLOW-UPS

What the interviewer asks when you finish early:

  • Groups: balances per group and overall. Where do you keep the ledger?
  • Editing or deleting an expense months later without corrupting history.

SEE ALSO