Design Splitwise
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-02-01
Problem Statement
Design and implement a simplified expense sharing system (like Splitwise) that allows users to manage group expenses, split bills, track balances, and settle up with each other.
Functional Requirements (FR)
User Management
•Users can register/login.
•Each user has a profile (name, email, contact info).
Groups & Friendships
•Users can add friends.
•Users can create groups (e.g., “Trip to Goa”, “Flatmates”).
•A group can have multiple members.
Expense Management
•Any user can add an expense for a group or among friends.
•Each expense contains → payer, participants, amount, date, description.
•Expenses can be split in multiple ways:
•Equal Split (everyone pays equally)
•Exact Amounts (custom amount per person)
•Percentage Split (percentage-based)
•System automatically calculates who owes whom.
Balance & Settlement
•Show per-user balances (who owes how much).
•Support simplified debts (minimizing number of transactions).
•Users can record a payment/settlement (cash, UPI, bank transfer).
Reports
•View expenses by group, by user, or history.
•Download/share summary.
Non-Functional Requirements (NFR)
•Scalability → Support millions of users and groups.
•Consistency → Balances must always remain accurate.
•Reliability → Ensure expenses are not lost.
•Extensibility → Add support for currencies, recurring expenses, and integrations with payment gateways.
•Security → Only authorized users can see group expenses.
Core Entities & Attributes
User
•userId, name, email, phone, groups[], friends[]
Group
•groupId, name, members[], expenses[]
Expense
•expenseId, groupId, payerId, participants[], amount, splitMethod (equal/exact/percentage), createdAt
Balance
•balanceId, userId, owedToUserId, amount
Transaction (Settlement)
•transactionId, payerId, payeeId, amount, method, date
APIs / Operations
User APIs
•registerUser(name, email, phone)
•addFriend(userId, friendId)
Group APIs
•createGroup(name, members[])
•addMember(groupId, userId)
•getGroupDetails(groupId)
Expense APIs
•addExpense(groupId, payerId, participants[], amount, splitMethod)
•getGroupExpenses(groupId)
•getUserExpenses(userId)
Balance APIs
•getBalances(userId)
•simplifyDebts(groupId)
Settlement APIs
•recordPayment(payerId, payeeId, amount)
•getTransactionHistory(userId)
LLD Design Goals
•Class Diagram → Entities: User, Group, Expense, Balance, Transaction, ExpenseManager.
•Design Patterns:
•Strategy Pattern → for split methods (equal, exact, percentage).
•Observer Pattern → for notifications (when new expense is added).
•Singleton Pattern → for central ExpenseManager to handle balances.
•Handle trade-offs:
•Optimizing for frequent reads (balances) vs. frequent writes (expenses).
•Balances stored pre-computed vs. calculated on demand.