Bird
Raised Fist0
LLDsystem_design~25 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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

Practice

(1/5)
1. What is the main purpose of using enum types like VehicleType and SpotType in system design?
easy
A. To group related constant values and prevent invalid inputs
B. To store large amounts of data efficiently
C. To perform complex mathematical calculations
D. To create user interface elements dynamically

Solution

  1. Step 1: Understand enum purpose

    Enums group related constant values, making code clearer and safer.
  2. Step 2: Identify benefits in system design

    They prevent invalid values by restricting inputs to predefined options.
  3. Final Answer:

    To group related constant values and prevent invalid inputs -> Option A
  4. Quick Check:

    Enums = group constants + prevent errors [OK]
Hint: Enums group fixed options to avoid mistakes [OK]
Common Mistakes:
  • Thinking enums store large data
  • Confusing enums with data structures
  • Using enums for UI elements
2. Which of the following is the correct syntax to define an enum VehicleType with values CAR, BIKE, and TRUCK?
easy
A. enum VehicleType = [CAR, BIKE, TRUCK]
B. enum VehicleType { CAR, BIKE, TRUCK }
C. VehicleType = enum(CAR, BIKE, TRUCK)
D. enum VehicleType: CAR, BIKE, TRUCK

Solution

  1. Step 1: Recall enum declaration syntax

    Standard enum syntax uses curly braces with comma-separated values.
  2. Step 2: Match syntax to options

    enum VehicleType { CAR, BIKE, TRUCK } matches the correct syntax: enum VehicleType { CAR, BIKE, TRUCK }.
  3. Final Answer:

    enum VehicleType { CAR, BIKE, TRUCK } -> Option B
  4. Quick Check:

    Enum syntax = curly braces + commas [OK]
Hint: Enums use braces and commas for values [OK]
Common Mistakes:
  • Using equal sign instead of braces
  • Using square brackets for enums
  • Missing commas between values
3. Given the enum SpotType { COMPACT, LARGE, HANDICAPPED } and a function that assigns spots based on vehicle type, what will be the output of this code snippet?
VehicleType vehicle = VehicleType.CAR;
SpotType spot;
switch(vehicle) {
  case VehicleType.CAR:
    spot = SpotType.COMPACT;
    break;
  case VehicleType.BIKE:
    spot = SpotType.HANDICAPPED;
    break;
  default:
    spot = SpotType.LARGE;
}
print(spot);
medium
A. Error: VehicleType not handled
B. HANDICAPPED
C. COMPACT
D. LARGE

Solution

  1. Step 1: Identify vehicle type value

    VehicleType is set to CAR.
  2. Step 2: Follow switch-case logic

    For CAR, spot is assigned COMPACT.
  3. Final Answer:

    COMPACT -> Option C
  4. Quick Check:

    CAR maps to COMPACT spot [OK]
Hint: Match enum cases carefully in switch [OK]
Common Mistakes:
  • Confusing BIKE case with CAR
  • Assuming default runs for CAR
  • Ignoring break statements
4. Consider this enum usage snippet:
enum VehicleType { CAR, BIKE, TRUCK }

void assignSpot(VehicleType v) {
  if (v == VehicleType.CAR) {
    print("Compact Spot");
  } else if (v == VehicleType.BIKE) {
    print("Bike Spot");
  }
}

What is the problem if assignSpot(VehicleType.TRUCK) is called?
medium
A. Prints "Bike Spot" incorrectly
B. Syntax error due to missing TRUCK case
C. Runtime error because TRUCK is invalid
D. No output because TRUCK case is not handled

Solution

  1. Step 1: Analyze if-else conditions

    Only CAR and BIKE cases are handled explicitly.
  2. Step 2: Consider TRUCK input

    TRUCK does not match any condition, so no print occurs.
  3. Final Answer:

    No output because TRUCK case is not handled -> Option D
  4. Quick Check:

    Unhandled enum value = no output [OK]
Hint: Always handle all enum values or add default [OK]
Common Mistakes:
  • Expecting syntax or runtime error
  • Assuming default print happens
  • Confusing enum invalidity
5. In a parking system, you have enums VehicleType { CAR, BIKE, TRUCK } and SpotType { COMPACT, LARGE, HANDICAPPED }. You want to assign spots such that:
- Cars use COMPACT spots
- Bikes use HANDICAPPED spots
- Trucks use LARGE spots

Which design approach best ensures no invalid spot assignment happens?
hard
A. Use a mapping dictionary from VehicleType to SpotType and validate assignments
B. Use separate if-else blocks without enums for flexibility
C. Assign spots randomly and check later if valid
D. Use strings instead of enums for vehicle and spot types

Solution

  1. Step 1: Understand enum benefits for mapping

    Enums provide fixed sets, so mapping ensures clear, valid assignments.
  2. Step 2: Evaluate design options

    Mapping dictionary with enums enforces rules and prevents invalid spots.
  3. Final Answer:

    Use a mapping dictionary from VehicleType to SpotType and validate assignments -> Option A
  4. Quick Check:

    Mapping enums = safe, clear assignments [OK]
Hint: Map enums directly to enforce valid pairs [OK]
Common Mistakes:
  • Using strings loses type safety
  • Random assignment causes errors
  • If-else without enums is error-prone