Design an expense-sharing app
Design the classes for an app where friends record shared expenses and see who owes whom.
Good first question for: SDE-1 · Also asked as: Design Splitwise
Requirements to confirm first
- Add an expense paid by one user and shared by several
- Split types: equal, exact amounts, percentages
- Show each user's balance with every other user
- Settle up: record a payment between two users
- Ask: groups or only pairs? One currency? Should debts be simplified?
Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.
Show the answer key (write yours first)
Classes and what each one owns
- User
- An id and a name
- Expense
- The amount, who paid, the list of splits and a description
- Split
- One participant and the amount they owe for this expense
- SplitStrategy
- Interface that turns the total and the inputs into validated splits
- BalanceSheet
- The net amount between every pair of users
- Group
- Members and their expenses
- ExpenseService
- Validates and stores an expense, then updates the balances
How they connect
- Expense has many Splits, and the splits must add up to the expense amount
- BalanceSheet is derived from expenses and settlements and can always be rebuilt from them
- ExpenseService picks a SplitStrategy from the split type
Design patterns that fit, and why
- Strategy
- Equal, exact and percentage differ only in how the splits are computed and validated.
- Factory
- Maps the split type in the request to the right strategy in one place.
- Observer
- Members are notified of a new expense without the expense code knowing about notifications.
Code sketch in Java
The core types and one real method. It is a sketch to talk through, not a full program.
interface SplitStrategy {
List<Split> split(long totalPaise, List<User> people, List<Long> inputs);
}
class EqualSplit implements SplitStrategy {
public List<Split> split(long total, List<User> people, List<Long> unused) {
long share = total / people.size(), extra = total % people.size();
List<Split> out = new ArrayList<>();
for (int i = 0; i < people.size(); i++)
out.add(new Split(people.get(i), share + (i < extra ? 1 : 0))); // hand out the remainder
return out;
}
}
class BalanceSheet {
private final Map<String, Map<String, Long>> owes = new HashMap<>(); // debtor -> creditor -> paise
}Edge cases to raise yourself
- 100 split among 3: the shares must still add up to exactly 100
- Percentages that do not add up to 100, or exact amounts that do not add up to the total
- The payer is also a participant and does not owe themselves
- Editing or deleting an old expense must reverse its effect on the balances
Where answers usually go wrong
- Floating-point money. A third of 100, three times, is not 100. Use integers in the smallest unit and hand out the remainder.
- An if-chain on the split type inside Expense. A new split type, such as by shares, should be a new class.
- Storing only balances and not the expenses. Then nothing can be edited, audited or rebuilt.
- Jumping to 'fewest transactions' before the basic model works. It is a follow-up, the exact minimum is hard to compute in general, and matching the largest debtor with the largest creditor is the answer usually expected.
Follow-up questions
- Simplify debts inside a group
- Add several currencies
- Two people add expenses at the same moment: what must be atomic?