| Users / Vehicles | 100 Vehicles | 10,000 Vehicles | 1,000,000 Vehicles | 100,000,000 Vehicles |
|---|---|---|---|---|
| Parking Slots | Small lot, ~100 slots | Large lot, multiple floors | Multiple lots, city-wide | Nation-wide, multi-city |
| Entry/Exit Points | 1-2 gates | 10+ gates with sensors | Hundreds of gates, automated | Thousands of gates, distributed control |
| Real-time Tracking | Manual or simple sensors | Automated sensors, cameras | Integrated IoT devices, cloud | Distributed systems, edge computing |
| Data Storage | Local DB or file system | Centralized DB with caching | Distributed DB, sharding | Multi-region DB clusters |
| User Interaction | Simple app or kiosk | Mobile apps, web portals | High concurrency, APIs | Global scale, multi-tenant |
Why parking lot is a classic LLD problem - Scalability Evidence
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the database that tracks parking slot availability and vehicle entries/exits. At small scale, a single database can handle updates and queries. As users grow, the number of concurrent updates and reads increases, causing delays and potential data inconsistency.
- Database Scaling: Use read replicas to handle queries, write sharding to distribute updates by parking zones.
- Caching: Cache slot availability to reduce DB reads.
- Horizontal Scaling: Add more application servers behind load balancers to handle user requests.
- Partitioning: Divide parking lots into zones managed independently to reduce contention.
- Edge Computing: Use local controllers at gates to process data before sending to central servers.
- Requests per second: For 10,000 vehicles with average 1 entry/exit per hour, ~3 requests/sec.
- Storage: Each vehicle record ~1 KB, 1M vehicles = ~1 GB storage.
- Bandwidth: Entry/exit data small (~100 bytes), 3 req/sec = ~300 bytes/sec, negligible network load.
Start by explaining the core entities: vehicles, slots, gates. Discuss how real-time updates and concurrency affect design. Identify bottlenecks like database and network. Propose scaling solutions step-by-step, focusing on data partitioning and caching. Use simple analogies like managing parking zones as neighborhoods.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add read replicas to distribute query load and implement caching for frequent reads to reduce database pressure.
Practice
Solution
Step 1: Understand the core challenge of parking lot design
The problem requires managing different vehicle types (cars, bikes, trucks) and matching them to appropriate parking spots with specific rules.Step 2: Identify why this fits LLD learning
This involves object modeling, resource allocation, and rule enforcement, which are key LLD concepts.Final Answer:
Because it involves managing different types of vehicles and parking spots with clear rules -> Option DQuick Check:
Parking lot = resource allocation + object modeling [OK]
- Thinking it's mainly about UI or fees
- Confusing with database or front-end problems
- Ignoring the variety of vehicle and spot types
Solution
Step 1: Identify proper class structure for parking spot
A parking spot should be modeled as a class with attributes like spot number, type, and occupancy status.Step 2: Evaluate options for correctness
class ParkingSpot { int spotNumber; String spotType; boolean isOccupied; } defines a class with clear attributes, suitable for LLD. Others are either functions, arrays, or invalid expressions.Final Answer:
class ParkingSpot { int spotNumber; String spotType; boolean isOccupied; } -> Option BQuick Check:
Class with attributes = correct parking spot model [OK]
- Using arrays or functions instead of classes
- Mixing data types incorrectly
- Not including occupancy status
class ParkingLot:
def __init__(self):
self.spots = {"car": 2, "bike": 1}
def park_vehicle(self, vehicle_type):
if self.spots.get(vehicle_type, 0) > 0:
self.spots[vehicle_type] -= 1
return "Parked"
else:
return "Full"
lot = ParkingLot()
print(lot.park_vehicle("car"))
print(lot.park_vehicle("car"))
print(lot.park_vehicle("car"))Solution
Step 1: Analyze initial spot counts and park_vehicle calls
Initially, car spots = 2. First call parks a car, spots become 1. Second call parks another car, spots become 0. Third call finds no spots left.Step 2: Determine output for each print statement
First two prints output "Parked", third outputs "Full" because no spots remain.Final Answer:
Parked Parked Full -> Option AQuick Check:
Spot count decreases, last attempt fails [OK]
- Assuming infinite spots
- Ignoring spot decrement
- Confusing vehicle types
class ParkingLot:
def __init__(self):
self.spots = {"car": 2}
def park_vehicle(self, vehicle_type):
if self.spots[vehicle_type] > 0:
self.spots[vehicle_type] = 0
return "Parked"
else:
return "Full"
lot = ParkingLot()
print(lot.park_vehicle("car"))
print(lot.park_vehicle("car"))Solution
Step 1: Review spot count update logic
The code sets spots[vehicle_type] = 0 directly instead of subtracting 1. With 2 initial car spots, first park sets it to 0 (should be 1).Step 2: Understand impact on multiple park calls
First park: "Parked", spots=0. Second park: "Full" but should succeed if decremented properly.Final Answer:
The spot count is set to 0 instead of decrementing by 1 -> Option CQuick Check:
Spot count update should decrement, not assign zero [OK]
- Ignoring decrement logic
- Assuming spots dictionary must have all vehicle types
- Overlooking return statements
Solution
Step 1: Consider object-oriented design principles
Using separate classes with a common interface allows clear modeling of different vehicle and spot types and their behaviors.Step 2: Evaluate scalability and maintainability
This approach supports adding new vehicle types easily and keeps code clean, unlike monolithic classes or random assignments.Final Answer:
Create separate classes for each vehicle and spot type with a common interface for parking logic -> Option AQuick Check:
Use OOP with interfaces for scalable parking lot design [OK]
- Using one class with many conditions
- Ignoring scalability needs
- Delaying design for other vehicle types
