0
0
LLDsystem_design~25 mins

Split strategies (equal, exact, percentage) in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Bill Splitter System
Design focuses on the core logic and architecture for split strategies and participant management. UI design, payment processing, and user authentication are out of scope.
Functional Requirements
FR1: Allow users to split a bill among multiple participants using different strategies: equal, exact, and percentage.
FR2: Equal split divides the total amount equally among all participants.
FR3: Exact split allows specifying the exact amount each participant pays.
FR4: Percentage split allows specifying the percentage of the total amount each participant pays.
FR5: Validate that the sum of exact amounts or percentages matches the total bill amount.
FR6: Support adding, updating, and removing participants and their split details.
FR7: Provide a summary of each participant's payable amount.
FR8: Handle bills with up to 100 participants.
Non-Functional Requirements
NFR1: The system should respond within 200ms for split calculations.
NFR2: Support concurrent updates from multiple users on the same bill with consistency.
NFR3: Ensure data accuracy and validation to prevent incorrect splits.
NFR4: Availability target of 99.9% uptime.
NFR5: Memory usage should be optimized for mobile and web clients.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Bill management module
Split strategy handler
Validation service
Participant management
API layer for client interaction
Data storage for bills and participants
Design Patterns
Strategy pattern for split calculation
Observer pattern for updates and notifications
Transaction management for concurrent updates
Validation and error handling patterns
Reference Architecture
  +-------------------+       +---------------------+       +-------------------+
  |                   |       |                     |       |                   |
  |   Client (UI)     +------>+  API Layer           +------>+  Split Strategy    |
  |                   | REST  |  (REST/GraphQL)      |       |  Handler          |
  +-------------------+       +---------------------+       +---------+---------+
                                                                       |
                                                                       v
                                                              +-------------------+
                                                              | Validation Service |
                                                              +---------+---------+
                                                                        |
                                                                        v
                                                              +-------------------+
                                                              |  Data Storage      |
                                                              | (Relational DB)    |
                                                              +-------------------+
Components
Client (UI)
Web/Mobile frontend
Interface for users to input bill details, select split strategy, and view results.
API Layer
REST or GraphQL API
Handles client requests, routes to appropriate services, and returns responses.
Split Strategy Handler
Backend service implementing Strategy pattern
Calculates split amounts based on selected strategy (equal, exact, percentage).
Validation Service
Backend validation module
Ensures correctness of splits, sums, and participant data before saving.
Data Storage
Relational Database (e.g., PostgreSQL)
Stores bills, participants, split details, and history.
Request Flow
1. User inputs bill total and participant details via Client UI.
2. Client sends request to API Layer with split strategy and participant data.
3. API Layer forwards data to Split Strategy Handler.
4. Split Strategy Handler calculates each participant's payable amount based on strategy:
5. - Equal: divides total by number of participants.
6. - Exact: uses specified amounts per participant.
7. - Percentage: calculates amounts from percentages.
8. Validation Service checks if sums match total and data is consistent.
9. If validation passes, data is saved to Data Storage.
10. API Layer returns split summary to Client UI for display.
Database Schema
Entities: - Bill: id (PK), total_amount, currency, created_at, updated_at - Participant: id (PK), bill_id (FK), name, contact_info - SplitDetail: id (PK), participant_id (FK), split_type (enum: equal, exact, percentage), amount, percentage Relationships: - One Bill has many Participants - Each Participant has one SplitDetail Constraints: - Sum of SplitDetail.amount per Bill must equal Bill.total_amount - For percentage splits, sum of SplitDetail.percentage per Bill must be 100%
Scaling Discussion
Bottlenecks
High concurrency when multiple users update the same bill simultaneously causing race conditions.
Validation logic becoming slow with large number of participants (close to 100).
Database write contention on bill and participant tables.
API Layer becoming a bottleneck under heavy load.
Solutions
Implement optimistic locking or transactions to handle concurrent updates safely.
Optimize validation logic with early exits and caching intermediate results.
Use database indexing and partitioning to improve write performance.
Scale API Layer horizontally with load balancers and stateless services.
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying assumptions, 20 minutes designing architecture and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain different split strategies clearly with examples.
Describe how Strategy pattern helps in clean design and extensibility.
Discuss validation importance and how to handle errors.
Highlight concurrency challenges and solutions.
Mention scalability considerations and how to maintain low latency.