Bird
0
0
LLDsystem_design~7 mins

Why parking lot is a classic LLD problem - Why This Architecture

Choose your learning style9 modes available
Problem Statement
Managing a parking lot manually leads to confusion and errors when tracking available spots, vehicle types, and fees. Without a clear system, it becomes hard to handle different vehicle sizes, multiple entry and exit points, and billing accurately.
Solution
A parking lot system models real-world entities like vehicles, parking spots, and tickets as classes with clear responsibilities. It uses object-oriented design to handle different vehicle types, assign spots, track occupancy, and calculate fees automatically.
Architecture
Vehicle
ParkingSpot
Ticket

This diagram shows how Vehicle, ParkingSpot, and ParkingLot classes interact, along with Ticket and FeeCalculator components to manage parking and billing.

Trade-offs
✓ Pros
Clear separation of concerns makes the system easy to extend for new vehicle types or fee rules.
Object-oriented design models real-world entities intuitively, improving maintainability.
Supports multiple entry/exit points and dynamic spot assignment efficiently.
✗ Cons
Initial design complexity can be high for beginners due to multiple interacting classes.
Overhead of managing many objects may affect performance in very large parking lots.
Requires careful handling of concurrency if multiple users access the system simultaneously.
Use when building software to manage parking lots with multiple vehicle types, dynamic spot allocation, and billing, especially if the system must scale beyond a few dozen spots.
Avoid if the parking lot is very small (under 10 spots) or if manual tracking is sufficient and no software automation is needed.
Real World Examples
Uber
Uber uses parking lot management logic in their driver hubs to allocate parking spots dynamically based on vehicle type and availability.
Amazon
Amazon's warehouse parking systems use similar object-oriented designs to track employee and delivery vehicle parking efficiently.
LinkedIn
LinkedIn's office parking management software models vehicles and spots to optimize space usage and billing for reserved spots.
Code Example
The before code mixes all logic in one class and uses a simple list to track spots, which is hard to extend. The after code uses separate classes for Vehicle and ParkingSpot, encapsulating behavior and making it easier to add features like different spot types or vehicle sizes.
LLD
### Before: No clear design, everything in one function
class ParkingLot:
    def __init__(self, capacity):
        self.capacity = capacity
        self.spots = [None] * capacity

    def park(self, vehicle):
        for i in range(self.capacity):
            if self.spots[i] is None:
                self.spots[i] = vehicle
                return i
        return -1

### After: Object-oriented design with classes
class Vehicle:
    def __init__(self, license_plate, vehicle_type):
        self.license_plate = license_plate
        self.vehicle_type = vehicle_type

class ParkingSpot:
    def __init__(self, spot_id, spot_type):
        self.spot_id = spot_id
        self.spot_type = spot_type
        self.is_free = True
        self.vehicle = None

    def park_vehicle(self, vehicle):
        if self.is_free and self.spot_type == vehicle.vehicle_type:
            self.vehicle = vehicle
            self.is_free = False
            return True
        return False

    def remove_vehicle(self):
        self.vehicle = None
        self.is_free = True

class ParkingLot:
    def __init__(self, spots):
        self.spots = spots

    def park_vehicle(self, vehicle):
        for spot in self.spots:
            if spot.park_vehicle(vehicle):
                return spot.spot_id
        return -1
OutputSuccess
Alternatives
Procedural Design
Uses functions and data structures without encapsulating behavior in objects.
Use when: Choose when the system is very simple and does not require extensibility or complex interactions.
Database-Centric Design
Focuses on database tables and queries rather than object models.
Use when: Choose when the main concern is data storage and retrieval, and business logic is minimal.
Summary
Parking lot design models real-world entities to manage spot allocation and billing effectively.
Object-oriented design improves maintainability and extensibility for complex parking systems.
It is a classic beginner problem to learn how to break down requirements into interacting classes.