Handling user accounts, debts, settlements
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-07-13
Problem Statement
Design and implement a system to manage user accounts, shared expenses, debts, and settlements. The system should allow multiple users to record debts, view balances, and settle payments efficiently.
Functional Requirements (FR)
User Management
•Users can register/login.
•Each user has a profile (name, email, currency).
•Maintain friends or group associations for expense sharing.
Debt Management
•Record debts between users: who owes whom, how much, and in which currency.
•Support recurring or one-time debts.
•Calculate net balances between two users to avoid duplicate tracking.
Settlement Management
•Users can settle debts partially or fully.
•Track settlement history.
•Support multiple payment methods: cash, bank transfer, wallet, or integration with payment gateways.
•Optimize settlements to minimize the number of transactions in a group.
Notifications
•Notify users when a debt is recorded, updated, or settled.
•Reminders for pending settlements.
Reports & Analytics
•View balances per user or group.
•Generate summaries of total owed, settled, or overdue amounts.
Non-Functional Requirements (NFR)
•Consistency → Balances must be accurate at all times.
•Concurrency → Handle simultaneous debt updates or settlements.
•Scalability → Support thousands of users and millions of transactions.
•Extensibility → Future support for multi-currency, automated reminders, or integrations with external payment APIs.
•Security → Encrypt sensitive user and financial data.
Core Entities & Attributes
User
•userId, name, email, currency, friendsList
Debt
•debtId, fromUserId, toUserId, amount, currency, timestamp, status (PENDING, SETTLED)
Settlement
•settlementId, debtId, amount, method, timestamp, status
Group (Optional)
•groupId, name, members[], debts[]
APIs / Operations
User APIs
•createUser(name, email, currency)
•getUserBalance(userId)
•addFriend(userId, friendId)
Debt APIs
•recordDebt(fromUserId, toUserId, amount, currency)
•getDebts(userId)
•updateDebt(debtId, newAmount)
Settlement APIs
•settleDebt(debtId, amount, method)
•getSettlementHistory(userId)
•optimizeSettlements(groupId)
Notifications APIs
•notifyUser(userId, message)
LLD Design Goals
•Class Diagram → Entities: User, Debt, Settlement, Group, DebtManager, SettlementManager, NotificationService.
•Design Patterns:
•Observer Pattern → For notifying users of updates or reminders.
•Strategy Pattern → For different settlement/payment methods.
•Singleton Pattern → For central DebtManager to manage all debts.
•Concurrency Handling → Ensure atomic updates for debts and settlements to prevent inconsistencies.
•Trade-offs → Pre-compute net balances vs. calculate on demand; real-time notifications vs. batched notifications.