Expense Sharing Application

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-01-10

Problem Statement

Design and implement an Expense Sharing System where multiple users can track, split, and settle shared expenses. The system should support different splitting strategies, group management, and optimized settlements.


Functional Requirements (FR)


User Management


•Register/login users with email/phone.
•Manage profile (name, default currency, friends list).


Group Management


•Create and manage groups (friends, roommates, trips).
•Add/remove members.
•View group balances.


Expense Management


•Add expense with details: description, amount, payer, participants, category, date.
•Split expenses in multiple ways:
•Equal split
•Exact amounts
•Percentage-based
•Custom ratios
•Support recurring expenses.
•Update/delete an expense.


Balance & Settlement


•Track debts: who owes whom.
•Settle debts (cash, UPI, card, or external integration).
•Optimize settlements to minimize number of transactions.


Notifications


•Notify users of new expenses, pending debts, and settlements.
•Send reminders for overdue payments.


Reports & Analytics


•View expense history for user/group.
•Categorize and filter by time, type, or category.
•Generate summary reports.


Non-Functional Requirements (NFR)


•Accuracy → Balances must always be consistent.
•Scalability → Handle thousands of groups and millions of expenses.
•Concurrency → Support simultaneous updates (two members adding expenses).
•Security → Protect financial data and transactions.
•Extensibility → Future support for OCR-based receipt scanning, multiple currencies, or payment gateway integration.


Core Entities & Attributes


User


•userId, name, email, phone, defaultCurrency, friendsList


Group


•groupId, groupName, members, createdAt


Expense


•expenseId, groupId, payerId, amount, participants, splitType, category, timestamp


Debt/Balance


•debtId, fromUserId, toUserId, amount, currency, lastUpdated


Payment


•paymentId, fromUserId, toUserId, amount, method, status, timestamp


APIs / Operations


User APIs


•createUser(name, email, phone)
•addFriend(userId, friendId)
•getUserBalance(userId)


Group APIs


•createGroup(name, members)
•addMember(groupId, userId)
•getGroupBalance(groupId)


Expense APIs


•addExpense(groupId, payerId, amount, participants, splitType)
•updateExpense(expenseId, newDetails)
•getExpenseHistory(groupId/userId)


Settlement APIs


•settleDebt(fromUserId, toUserId, amount, method)
•optimizeSettlements(groupId)


LLD Design Goals


•Class Diagram → Entities like User, Group, Expense, Debt, Payment, NotificationService.
•Design Patterns:
•Strategy Pattern → for splitting logic (equal, exact, percentage, ratio).
•Observer Pattern → for notifications.
•Factory Pattern → for payment methods.
•Handle trade-offs: Optimized settlement algorithm (greedy vs. min-flow), group-level vs. global balances, consistency vs. performance.


Solution

Comments