This system calculates fines for users based on predefined rules such as late returns or violations. It must handle multiple fine types, apply correct rates, and provide results quickly and accurately.
Fine calculation in LLD - Architecture Diagram
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
System Overview - Fine calculation
Architecture Diagram
User | v Load Balancer | v API Gateway | v Fine Calculation Service | | v v Cache Database | | v v Response Fine Rules Storage
Components
User
user
Initiates fine calculation requests
Load Balancer
load_balancer
Distributes incoming requests evenly to services
API Gateway
api_gateway
Handles request routing, authentication, and rate limiting
Fine Calculation Service
service
Processes fine calculation logic using rules and user data
Cache
cache
Stores recent fine calculation results for quick retrieval
Database
database
Stores user data and fine calculation history
Fine Rules Storage
database
Stores fine calculation rules and rates
Response
response
Delivers fine calculation results back to the user
Request Flow - 11 Hops
User → Load Balancer
Load Balancer → API Gateway
API Gateway → Fine Calculation Service
Fine Calculation Service → Cache
Cache → Fine Calculation Service
Fine Calculation Service → Database
Fine Calculation Service → Fine Rules Storage
Fine Calculation Service → Cache
Fine Calculation Service → API Gateway
API Gateway → Load Balancer
Load Balancer → User
Failure Scenario
Component Fails:Cache
Impact:Fine Calculation Service must query database for every request, increasing latency
Mitigation:System continues working by fetching data from database; cache can be restored or replaced without downtime
Architecture Quiz - 3 Questions
Test your understanding
Which component first handles the user's fine calculation request?
Design Principle
Practice
1.
What is the primary purpose of a fine calculation system in low-level design?
easy
Solution
Step 1: Understand the system goal
The fine calculation system is designed to handle rule violations and compute the corresponding charges automatically.Step 2: Identify the main function
Its main function is to calculate fines based on violation details and fixed rates.Final Answer:
To automatically compute charges for rule violations -> Option AQuick Check:
Fine calculation = automatic charge computation [OK]
Hint: Focus on the system's main task: charging fines [OK]
Common Mistakes:
- Confusing fine calculation with user management
- Thinking it handles authentication
- Assuming it generates performance reports
2.
Which of the following is the correct way to represent a fine rate for a violation type in a configuration file?
violation_fine_rates = {'speeding': 100,'parking': 50,'signal_jump': 150}
easy
Solution
Step 1: Analyze the data structure
The example shows a dictionary mapping violation names to their fine amounts, which is clear and easy to update.Step 2: Compare with other options
Lists or strings do not map violation types to amounts directly, and booleans cannot store fine values.Final Answer:
Using a dictionary with violation types as keys and fine amounts as values -> Option CQuick Check:
Dictionary maps violation to fine [OK]
Hint: Use key-value pairs for clear violation-to-fine mapping [OK]
Common Mistakes:
- Using lists without keys loses violation context
- Using strings cannot store amounts
- Booleans cannot represent fine values
3.
Given the following code snippet, what will be the total fine calculated?
violation_fine_rates = {'speeding': 100, 'parking': 50}violations = ['speeding', 'parking', 'speeding']total_fine = sum(violation_fine_rates[v] for v in violations)print(total_fine)
medium
Solution
Step 1: Calculate fine for each violation
Violations are 'speeding', 'parking', 'speeding'. Their fines are 100, 50, and 100 respectively.Step 2: Sum all fines
Total fine = 100 + 50 + 100 = 250.Final Answer:
250 -> Option DQuick Check:
100 + 50 + 100 = 250 [OK]
Hint: Add fines for each violation in the list [OK]
Common Mistakes:
- Counting each violation only once
- Adding fines incorrectly
- Ignoring repeated violations
4.
Identify the error in the following fine calculation code snippet:
violation_fine_rates = {'speeding': 100, 'parking': 50}violations = ['speeding', 'parking', 'signal_jump']total_fine = sum(violation_fine_rates[v] for v in violations)print(total_fine)
medium
Solution
Step 1: Check dictionary keys against violations
'signal_jump' is not a key in violation_fine_rates, so accessing it causes a KeyError.Step 2: Understand error type
Attempting to access a missing key in a dictionary raises KeyError in Python.Final Answer:
KeyError occurs because 'signal_jump' is not in the rates dictionary -> Option BQuick Check:
Missing key access = KeyError [OK]
Hint: Check if all violation keys exist in the rates dictionary [OK]
Common Mistakes:
- Assuming missing keys return zero
- Confusing KeyError with SyntaxError
- Ignoring runtime errors
5.
You are designing a fine calculation system that must support multiple violation types, each with different fine rates and possible discounts for repeat offenses. Which design approach is best?
hard
Solution
Step 1: Identify need for flexible fine rates
Different violation types require different base fines, so a mapping structure is needed.Step 2: Incorporate discount logic
Discounts for repeat offenses require additional logic applied on top of base fines.Step 3: Choose design approach
A dictionary for base fines plus discount logic is clear, scalable, and easy to update.Final Answer:
Use a dictionary mapping violation types to base fines and add logic to apply discounts based on offense count -> Option AQuick Check:
Dictionary + discount logic = scalable design [OK]
Hint: Map base fines and add discount logic for repeats [OK]
Common Mistakes:
- Ignoring violation types in fine calculation
- Hardcoding fines without flexibility
- Not handling repeat offense discounts
