DesignTheSystem/dev
MEDIUM Low-Level Design (LLD) 13 automated tests

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.

DifficultyMEDIUM
Design focusStrategies for one operation, and exact money in whole cents
Automated grading3 public + 10 hidden tests (13 total)
Phase 2 follow-upSplit by shares
LanguageTypeScript (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 (core)

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 (edge)

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 (core)

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

More low-level design problems