Design Expense Sharing (Splitwise) (Low-Level Design)
Split shared expenses several ways, track who owes whom, and suggest the fewest payments to settle up. Focus: Strategies for one operation, and exact money in whole cents.
| Difficulty | MEDIUM |
|---|---|
| Design focus | Strategies for one operation, and exact money in whole cents |
| Automated grading | 3 public + 10 hidden tests (13 total) |
| Phase 2 follow-up | Split by shares |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Amounts are whole cents. A positive balance means others owe you; negative means you owe.
EQUAL splits divide evenly; leftover cents go one each to the first people listed. EXACT splits give each person a set amount, which must add up to the total. PERCENT splits must add up to 100; each share is rounded down and leftover cents go one each to the people in the order listed.
settleUp() suggests payments without changing balances: repeatedly the person who owes the most pays the person owed the most (ties by user id, alphabetically), as much as settles one of them.
Functional requirements
- addUser(id); addExpense(paidBy, amount, split) where split is { type: "EQUAL", among } or { type: "EXACT", amounts } or { type: "PERCENT", percents }.
- Throws for unknown users, amounts that are not positive whole numbers, or splits that do not add up.
- balance(user), balances() (every user, including 0), settleUp().
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
type Split =
| { type: "EQUAL"; among: string[] }
| { type: "EXACT"; amounts: Record<string, number> }
| { type: "PERCENT"; percents: Record<string, number> };
class ExpenseManager {
addUser(id: string): void {
throw new Error("Not implemented");
}
addExpense(paidBy: string, amount: number, split: Split): void {
throw new Error("Not implemented");
}
balance(user: string): number {
throw new Error("Not implemented");
}
balances(): Record<string, number> {
throw new Error("Not implemented");
}
settleUp(): { from: string; to: string; amount: number }[] {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 10 hidden edge-case tests in a sandbox:
Equal split
var m = new ExpenseManager();
["a", "b", "c"].forEach(function (u) { m.addUser(u); });
m.addExpense("a", 300, { type: "EQUAL", among: ["a", "b", "c"] });
assertEqual(m.balances(), { a: 200, b: -100, c: -100 });
Exact split must add up
var m = new ExpenseManager();
m.addUser("a"); m.addUser("b");
assertThrows(function () { m.addExpense("a", 100, { type: "EXACT", amounts: { a: 50, b: 40 } }); });
Settle up
var m = new ExpenseManager();
m.addUser("a"); m.addUser("b");
m.addExpense("a", 100, { type: "EQUAL", among: ["a", "b"] });
assertEqual(m.settleUp(), [{ from: "b", to: "a", amount: 50 }]);
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- Each split type is its own strategy: Adding SHARES should mean adding one class, not editing a growing if/else inside addExpense.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 13 tests, you unlock the change request: Split by shares.
Relevant low-level design lessons
Strategy: swap behaviour, not code
Replace an if/else on a type with interchangeable strategy classes.
Validate everything, then change
Make operations all-or-nothing so failures leave no half-done state.
Safe retries with request ids
Make a money operation safe to call twice.
More low-level design problems
Design Key-Value Store with Expiry
An in-memory store with per-key expiry, later with nested transactions.
Design Digital Wallet
Deposits, withdrawals and transfers that are safe to retry, with a full history.
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Rate Limiter
Two rate-limiting algorithms behind one interface, so callers can swap them.