Bird
Raised Fist0
LLDsystem_design~20 mins

Enum usage (VehicleType, SpotType) in LLD - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Enum Mastery in Parking System
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Enum Role in Parking Lot Design

In a parking lot system, VehicleType and SpotType enums are used. What is the main purpose of using enums here?

ATo restrict vehicle and spot categories to predefined constant values for clarity and safety.
BTo allow dynamic creation of vehicle and spot types at runtime.
CTo store vehicle and spot data in a database table.
DTo randomly assign vehicle types to spots.
Attempts:
2 left
💡 Hint

Think about how enums help avoid errors by limiting options.

Architecture
intermediate
1:30remaining
Mapping VehicleType to SpotType in Design

Which design best represents the relationship between VehicleType and SpotType enums in a parking lot system?

AEach VehicleType maps to one or more compatible SpotTypes to check parking eligibility.
BSpotType values are randomly assigned to VehicleType values without mapping.
CVehicleType and SpotType enums are merged into a single enum for simplicity.
DVehicleType enum contains SpotType values as its members.
Attempts:
2 left
💡 Hint

Consider how the system knows which vehicle fits which spot.

scaling
advanced
2:00remaining
Scaling Enum Usage for New Vehicle and Spot Types

When adding new vehicle and spot types frequently, what is the best approach to keep the enum usage scalable and maintainable?

ACreate a separate enum for each new vehicle and spot type pair.
BHardcode all new types directly into enums and recompile the system each time.
CRemove enums and use plain strings everywhere for flexibility.
DUse enums with extension mechanisms or configuration files to add new types without code changes.
Attempts:
2 left
💡 Hint

Think about how to avoid frequent code changes for new types.

tradeoff
advanced
2:00remaining
Tradeoffs Between Enum and Database Lookup for SpotType

What is a key tradeoff when choosing between using enums or a database table to represent SpotType in a parking system?

AEnums are slower to access than database queries.
BDatabase tables cannot represent SpotType relationships.
CEnums offer compile-time safety but less flexibility; database allows dynamic updates but adds latency.
DEnums require a database connection to function.
Attempts:
2 left
💡 Hint

Consider flexibility versus performance and safety.

estimation
expert
2:30remaining
Estimating Capacity for Enum-Based Parking Spot Allocation

A parking system uses enums for VehicleType and SpotType. Given 3 VehicleTypes and 4 SpotTypes, with each VehicleType compatible with 2 SpotTypes, estimate the minimum number of spot instances needed to support 100 vehicles simultaneously, assuming uniform distribution and no sharing.

AAt least 150 spots, assuming some spot types can serve multiple vehicle types.
BExactly 100 spots, one per vehicle regardless of type.
CAt least 200 spots, because each vehicle needs a unique compatible spot.
DAt least 300 spots, to cover all combinations of vehicle and spot types.
Attempts:
2 left
💡 Hint

Think about how many spots are needed if each vehicle must have a compatible spot without sharing.

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