Design Splitwise

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-22

Problem Statement

Design the Low-Level Design (LLD) for an expense-sharing application like Splitwise, where a group of friends can add expenses, split them among participants, and track who owes whom.


The goal is to model classes, relationships, and operations that enable efficient expense tracking and balance settlement — focusing on object-oriented design and clean extensibility, not

distributed systems or APIs.


🧠 Functional Requirements


User Management

•Users can be added to the system.
•Each user has a name, unique ID, email, and phone number.

Expense Creation

•Users can create expenses specifying:
•Total amount
•Payer (who paid)
•Participants
•Split type (Equal / Exact / Percentage)
•Example: “A paid ₹1200 for dinner, shared equally among A, B, C.”

Expense Splitting

•Support 3 primary split types:
•Equal Split → Everyone pays the same share.
•Exact Split → Each user’s contribution is manually specified.
•Percentage Split → Amount divided by percentage shares.

Balance Tracking

•Maintain a ledger to track:
•Who owes whom
•How much is owed
•Balances should be auto-updated after each expense.

Simplify Debts

•Provide a way to minimize the number of transactions required to settle up (optional feature).

Show Balances

•For each user:
•Show total amount owed and due.
•For groups or the whole system:
•Show pairwise balances.


🚫 Non-Functional Requirements

•Efficient updates — O(1) per expense for balance updates.
•Accurate floating-point handling (rounding to 2 decimal places).
•Extensible design for future features:
•Group-based expenses
•Multi-currency
•Notifications
•Settlements


🧱 Suggested Classes


ClassResponsibility


User

Represents an individual user with personal details.


Expense

Abstract base class representing a generic expense.


EqualExpense, ExactExpense, PercentageExpense

Concrete subclasses for different split types.


Split

Represents how much a particular user owes in an expense.


BalanceSheet

Maintains net balances for each user relative to others.


ExpenseManager

Central class responsible for adding expenses and updating balances.


Transaction (Optional)

Represents a settlement between two users.


🔄 Example Flow


1.Add Users:
1 2A = new User("A", "a@email.com", "9999") 3B = new User("B", "b@email.com", "8888") 4C = new User("C", "c@email.com", "7777")
1.Add Expense:
1 2ExpenseManager.addExpense( 3 payer = A, 4 amount = 1200, 5 participants = [A, B, C], 6 type = EQUAL 7)
1.Show Balances:
1 2B owes A ₹400 3C owes A ₹400
1.Add Another Expense:
1 2ExpenseManager.addExpense( 3 payer = B, 4 amount = 600, 5 participants = [A, B, C], 6 type = EQUAL 7)
1.Balances adjust automatically.
2.Show All Balances:
1 2A owes B ₹200 3C owes A ₹200

🧪 Example Input / Output


Input:

1 2Users = [A, B, C] 3Expense1: A pays 1200 (equal) 4Expense2: B pays 600 (equal)


Output:

1 2A owes B ₹200 3C owes A ₹200


⚙️ Constraints


•Maximum users: 10^4
•Maximum expenses: 10^6
•All amounts non-negative
•Precision: 2 decimal places
•Balances should be symmetric (if A owes B ₹100, then B owed A = -₹100)


🎯 Goals


Your design should:


•Clearly separate models (User, Expense, Split) from logic (ExpenseManager).
•Ensure scalability for adding/removing users and expenses.
•Support easy extension for:
•Groups
•Settlements
•Expense categories (Travel, Food, Rent, etc.)


✅ In short:


Design the classes and interactions for a Splitwise-like expense sharing system that supports multiple users, different split types, and real-time balance management.

Solution

Comments