Design a Splitwise-style Expense Sharing for Groups (Simplified)

Easy
Real World SystemsBalance TrackingEqual SplitDebt SimplificationGraph

Problem Statement

Design a simplified expense sharing tool for a group of friends. Users add expenses, view who owes whom, and settle debts. Focus on correct balance computation with equal splits only.

Requirements

Functional Requirements

  • Add a user to the system
  • Add an expense: who paid, how much, which users share it equally
  • getBalance(userId) → net balance (positive = owed money, negative = owes money)
  • getOwes(userA, userB) → how much A owes B (or B owes A)
  • settle(userA, userB) → record that A paid back B in full
  • List all outstanding debts in the group
  • Simplify debts: minimize number of transactions to settle all balances

Non-Functional Requirements

  • Simple in-memory storage
  • No concurrency required for this simplified version

Key Entities

  • User: id, name
  • Expense: id, payer, participants, amount, description, timestamp
  • BalanceSheet: Map<UserId, Map<UserId, Double>> — net amounts between each pair
  • ExpenseService: manages users, expenses, and balance computations

Constraints

  • Equal split only (amount / number of participants)
  • Settle clears balance between two users to zero
  • Self-paying expenses (only 1 participant) are ignored
  • Balances stored as net amounts per user pair

!Constraints

- Equal split only - Self-expenses (payer is sole participant) are not tracked - Settle operation zeros out the balance between two users - Balances are always stored as net (no duplicate entries for A↔B and B↔A) - Debt simplification is optional bonus
Output

Run or submit your code to see output here.