LLD Case Study: Expense Sharing (Splitwise)
Design an expense sharing app with groups, multiple split types, balance tracking and debt simplification.
Intermediate · about 4 hours · difficulty 3/5
Learn first: Approaching an LLD Problem: Requirements, Entities and UML
Splitwise is a popular machine coding question at Indian product companies because it mixes clean modelling with a small algorithm.
Requirements
- Users, and groups of users (a Goa trip, flatmates).
- Add an expense paid by one (or several) users, split among participants.
- Split types: EQUAL, EXACT amounts, PERCENT, and sometimes SHARES.
- Show balances: who owes whom and how much, per group and overall.
- Settle up, and optionally simplify debts to minimise the number of payments.
Model
User, Group, Expense (id, description, amount, paidBy, splits, type, createdAt), Split (user, amount) with subclasses or a type field, and a BalanceSheet keyed by pairs of users (or a net balance per user per group).
Patterns
- Strategy (or Factory plus Strategy) for split types: each validates its input (percentages sum to 100, exact amounts sum to the total) and computes the per-user amounts.
- Observer for notifying users when an expense is added.
- Keep money as integer paise (or a decimal type), never floating point. Decide where rounding remainders go (for example to the payer).
Debt simplification
Compute each user's net balance (total paid minus total owed). Then repeatedly match the largest creditor with the largest debtor and settle the smaller of the two amounts. This greedy approach, using two heaps, is simple and usually what interviewers expect. Finding the absolute minimum number of transactions is NP-hard in general, so mention that and stay with the greedy approach.
Pitfalls
- Floating point money: 100 / 3 split three ways leaves a paisa missing.
- Putting split validation in the controller instead of the split strategy.
- Storing only pairwise balances and getting them inconsistent after edits and deletions.
You are ready to move on when you can implement all four split types with validation, show balances, and simplify debts for a group of five people.
Key concepts
- Users, groups and expenses
- Split types as strategies
- Split validation
- Balance sheet
- Money as integer paise
- Greedy debt simplification
- Expense edit and delete
Mini project
Build a command-line Splitwise clone that accepts commands like EXPENSE u1 1000 4 u1 u2 u3 u4 EQUAL and SHOW u2, supports EQUAL, EXACT and PERCENT splits, and includes a SIMPLIFY command that prints the minimal greedy settlement list.
Check yourself
Three friends split Rs 100 equally. How do you store the amounts without losing a paisa, and who absorbs the remainder?
Free resources
- Awesome Low Level Design (GitHub)
- Strategy pattern (Refactoring.Guru)
- Factory method pattern (Refactoring.Guru)