Design Splitwise
💸 What is Splitwise? It is an app for friends who share costs. One person pays the dinner bill, the app records who owes them how much, and over a trip or a year of shared rent it keeps a running balance between every pair of people. When it is time to pay up, it suggests a short list of payments that clears everything.
Interviewers ask this in a few forms: "design an expense-sharing app", "support equal, exact and percentage splits", or "given who owes whom, settle everyone with as few payments as possible". They are one design. The split types decide how one expense turns into shares, the balances add those shares up over time, and debt simplification reads the balances at the end. We build all three.
Requirements
"Design Splitwise. People add expenses that are shared among a group, and the app shows who owes whom."
That is all most interviewers say. How an expense is split, what happens to odd cents and what "who owes whom" looks like are all yours to ask.
Clarifying Questions
You: How can one expense be split?
Interviewer: Three ways. Equally among the people listed; by exact amounts for each person; or by percentages that add up to 100.
You: Money is in cents, so 100 split three ways does not divide evenly. Who gets the extra cent?
Interviewer: Round every share down, then hand the left-over cents out one each, starting with the first person in the list. For percentages, skip people at 0 percent; they should never pay a cent.
You: Does the person who paid have to be one of the people sharing it?
Interviewer: No. If I buy a gift for two friends, I paid, but only they owe me.
You: What balances does a user see?
Interviewer: One line per friend: "you owe Bob 7 dollars" or "Bob owes you 3". If Alice owes Bob 5 and then Bob owes Alice 2, the app just shows Alice owes Bob 3. Also one total per user.
You: How do people pay each other back?
Interviewer: They record a payment, a "settle up". It can be more than what is owed, and then the balance flips.
You: What does simplifying debts mean exactly?
Interviewer: Suggest a list of payments that brings everyone to zero, with fewer payments than paying each pairwise balance. It is only a suggestion and does not change anything. Use a simple greedy rule: the person who owes the most pays the person who is owed the most.
You: Groups, currencies, editing expenses?
Interviewer: One shared space and one currency for now. We may talk about groups and edits later.
The rounding answer is the one people skip, and it is the one the tests catch. Splitwise is a problem about money, and money must add up exactly: the shares of an expense always sum to its amount, to the cent.
💡 Interview tip: Pick an example with an ugly number (100 cents among 3 people) and ask about it out loud. It shows you think about money as integers, and it turns a vague "split equally" into a precise rule you can code.
Final Requirements
- Users are identified by a unique id.
- An expense has a payer, an amount in cents, a list of people sharing it and a split type:
EQUAL,EXACT(amounts that sum to the total) orPERCENT(whole percentages that sum to 100). - Shares are rounded down to the cent; the left-over cents go one each to the first people in the list (for
PERCENT, skipping people at 0 percent). The shares always sum to the amount. - Everyone sharing an expense, except the payer, owes the payer their share. The payer need not be in the list.
- The app keeps a net balance for every pair of users, and a net total for every user.
- A user can record a payment to another user (settle up). Overpaying flips the balance.
- The app can suggest payments that bring everyone to zero, using the greedy rule: the biggest debtor pays the biggest creditor, ties broken by user id. Suggesting changes nothing.
- Invalid expenses (unknown users, duplicates, bad amounts, values that don't sum up) are rejected.
Out of scope
- Groups, multiple currencies, receipts and comments.
- Editing or deleting expenses (we come back to these in Extensibility).
- Moving real money. A settle-up only records that a payment happened.