Splitwise

Expense Sharing

Design an expense-sharing service: one person pays, the bill is split by a rule, balances are tracked in whole cents, and the group settles up with as few transfers as the greedy method finds.

Overview

A group of friends pays for things on each other's behalf: one buys dinner, another the train tickets. The service records each expense, splits it among the people who shared it, keeps a running balance of who owes whom, and at the end proposes a short list of payments that clears everything. Splitwise is the app that made this popular; its own description of debt simplification lists three rules: everyone owes the same net amount afterward, no one owes a person they did not owe before, and no one owes more in total than before.

The problem has two halves. The object model half is about splitting one expense correctly: equal, exact amounts or percentages, with every cent accounted for. The algorithm half is about settling: turning a tangle of pairwise debts into a few transfers. Interviewers use the first half to check that you model money without floats and use a strategy object for the split rule, and the second half to see whether you know the greedy method, its guarantee of at most N minus 1 transfers, and why finding the true minimum is hard.

A strong answer stores amounts as integer cents, validates each split before touching any balance, keeps both the expense log and a net balance per user, and can explain the greedy settlement with a small trace.

Scoping the problem

Ask these questions before you design. Each answer shapes a class, and each comes with the answer interviewers usually give.

  • Which split rules are needed? Each rule is a strategy class, so ask before writing the base class. Usually equal, exact amounts and percentages.
  • One currency or many? Mixed currencies need exchange rates and a currency per balance. Usually one currency, in whole cents.
  • Do we need groups? A group scopes expenses and settlement to its members. Usually yes, but the ledger logic is the same.
  • Is the payer always one person? Several payers make an expense a list of (payer, amount) pairs. Usually one payer.
  • Should settlement be per pair or simplified? Simplification changes who pays whom, not how much anyone owes in total. Offer both; simplification is the interesting part.
  • Many users adding at once? Two writes to the same balance must not race. Use a lock per group in memory, or a transaction in a database.

Functional requirements

  • Record an expense: who paid, the total in cents, the participants and the split rule
  • Split equally, by exact amounts or by percentages, with every cent assigned and shares summing to the total
  • Reject a split that does not add up before any balance changes
  • Keep a net balance per user and the full expense log; answer how much one user owes another
  • Produce a settlement: a list of transfers that brings every net balance to zero
  • Record a payment between two users as a settlement transfer

Non-functional requirements

  • Amounts are integers (cents); no floating point anywhere in the ledger
  • The net balances always sum to zero; the code asserts it after every change
  • Settlement runs in O(N log N) and returns at most N minus 1 transfers
  • Concurrent add_expense calls on one ledger are serialized by a lock
  • The expense log is never lost; balances can be rebuilt from it

Interview questions

  • Why integer cents instead of floats or Decimal?
  • Should the system store every expense, or just the balances?
  • Is the greedy settlement optimal? What would optimal cost?
  • How do you add a new split rule, for example splitting by shares?
  • Two people add expenses to the same group at the same time. What breaks, and how do you stop it?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio