Design Splitwise
viaLeetCode
Problem Object-oriented design of Splitwise: users, groups, shared expenses with multiple split strategies, and simplified who-owes-whom balances.
Requirements
- addExpense(payer, amount, participants, splitType, splitDetails) supporting EQUAL, EXACT amounts, and PERCENT splits (validating totals); per-pair running balances; group and non-group expenses; showBalance(user) and simplify-debts.
Core design
- Classes: User, Group(members, expenses), Expense(payer, amount, splits), Split hierarchy — EqualSplit/ExactSplit/PercentSplit — created via a SplitStrategy/factory keyed by split type (the pattern centerpiece: validation + share computation polymorphic per type).
- BalanceSheet: map of (debtor, creditor) → amount, updated per expense; ExpenseService orchestrating creation, validation, balance updates; observer hook for notifications.
- Simplify debts: net each user's total balance, then settle max-creditor against max-debtor repeatedly (heap two-pointer) — minimizes transaction count; know it's a greedy heuristic.
Discussion points
- Validation per strategy (exact sums = amount, percents = 100) enforced inside each Split class — OCP for adding SHARES/adjustment types later.
- Money handling: integers in minor units / BigDecimal, rounding-remainder assignment (who absorbs the extra cent on 100/3).
- Concurrency on shared balances, idempotent expense creation, and immutable expense history with corrections as new entries (audit) as senior probes.
asked …