Bird
0
0
LLDsystem_design~25 mins

Enum usage (VehicleType, SpotType) in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Parking Lot System with Enum Usage
Design focuses on enum usage for vehicle and spot types, spot assignment logic, and basic parking lot management. Payment, user management, and multi-level parking are out of scope.
Functional Requirements
FR1: Support different types of vehicles: Motorcycle, Car, Truck
FR2: Support different types of parking spots: MotorcycleSpot, CompactSpot, LargeSpot
FR3: Assign vehicles to appropriate spot types based on vehicle type
FR4: Prevent parking a vehicle in an incompatible spot
FR5: Allow querying available spots by spot type
FR6: Track occupied and free spots
Non-Functional Requirements
NFR1: System should handle up to 500 vehicles parked simultaneously
NFR2: Spot assignment and release operations should respond within 100ms
NFR3: System availability target: 99.9% uptime
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Enum definitions for VehicleType and SpotType
ParkingSpot manager to track spot availability
Vehicle class with type attribute
Assignment logic mapping VehicleType to allowed SpotTypes
Storage for spot occupancy status
Design Patterns
Enum pattern for fixed set of constants
Strategy pattern for spot assignment rules
Observer pattern for spot availability updates
Reference Architecture
  +----------------+       +------------------+       +----------------+
  |   Vehicle      |       |  ParkingSpot     |       |  ParkingLot     |
  | - vehicleType  |<----->| - spotType       |<----->| - spots[]       |
  +----------------+       | - isOccupied     |       | - assignSpot()  |
                           +------------------+       | - releaseSpot() |
                                                      +----------------+

Enums:
VehicleType = {Motorcycle, Car, Truck}
SpotType = {MotorcycleSpot, CompactSpot, LargeSpot}

Mapping:
Motorcycle -> MotorcycleSpot, CompactSpot, LargeSpot
Car -> CompactSpot, LargeSpot
Truck -> LargeSpot
Components
VehicleType Enum
Enum (language built-in)
Defines fixed vehicle categories: Motorcycle, Car, Truck
SpotType Enum
Enum (language built-in)
Defines fixed parking spot categories: MotorcycleSpot, CompactSpot, LargeSpot
Vehicle Class
Object-oriented class
Represents a vehicle with a VehicleType attribute
ParkingSpot Class
Object-oriented class
Represents a parking spot with SpotType and occupancy status
ParkingLot Manager
Service or class
Manages spot availability, assignment, and release
Request Flow
1. 1. Vehicle arrives with a VehicleType.
2. 2. ParkingLot queries available spots compatible with VehicleType.
3. 3. Assign the first available compatible spot to the vehicle.
4. 4. Mark the spot as occupied.
5. 5. When vehicle leaves, release the spot and mark it free.
Database Schema
Entities: - Vehicle (id, vehicle_type) - ParkingSpot (id, spot_type, is_occupied) Relationships: - Vehicle assigned to one ParkingSpot (1:1 when parked) VehicleType and SpotType are enums stored as strings or integers in the database.
Scaling Discussion
Bottlenecks
Single ParkingLot manager becomes a bottleneck with many concurrent spot assignments
Database contention when updating spot occupancy status
Latency increases when searching for available spots in large parking lots
Solutions
Partition parking lot into zones with separate managers to reduce contention
Use in-memory caching (e.g., Redis) for spot availability with eventual consistency to database
Index spots by type and availability for fast lookup
Implement optimistic locking or transactions to handle concurrent updates safely
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing enums and assignment logic, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain why enums are useful for fixed categories and type safety
Describe how vehicle types map to spot types and why this matters
Discuss how to handle spot assignment and release efficiently
Mention how to scale the system for larger parking lots
Highlight trade-offs between strict type matching and flexibility