What if a tiny typo could crash your whole parking system? Enums save you from that nightmare!
Why Enum usage (VehicleType, SpotType) in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine managing a parking lot system where you have to track different types of vehicles and parking spots manually using plain text or numbers.
Every time you add a new vehicle or spot type, you must remember the exact string or number and ensure consistency everywhere.
This manual approach is slow and error-prone because typos or mismatched values can cause bugs.
For example, writing "car" sometimes and "Car" other times leads to confusion and wrong behavior.
It also makes the code hard to read and maintain, especially as the system grows.
Using enums for VehicleType and SpotType gives a fixed set of named values that the system understands clearly.
This prevents mistakes, makes the code easier to read, and helps the system handle only valid types.
It acts like a checklist that the program follows strictly, avoiding guesswork.
vehicleType = "car" if vehicleType == "car": parkCar()
enum VehicleType { CAR, BIKE, TRUCK }
vehicleType = VehicleType.CAR
if vehicleType == VehicleType.CAR:
parkCar()It enables building reliable and clear systems that handle specific categories without confusion or errors.
In a parking management app, enums ensure that only valid vehicle types like CAR, BIKE, or TRUCK are accepted, preventing wrong inputs and simplifying spot allocation.
Manual string or number codes cause errors and confusion.
Enums provide a fixed, clear set of valid types.
This leads to safer, easier-to-maintain system design.
Practice
enum types like VehicleType and SpotType in system design?Solution
Step 1: Understand enum purpose
Enums group related constant values, making code clearer and safer.Step 2: Identify benefits in system design
They prevent invalid values by restricting inputs to predefined options.Final Answer:
To group related constant values and prevent invalid inputs -> Option AQuick Check:
Enums = group constants + prevent errors [OK]
- Thinking enums store large data
- Confusing enums with data structures
- Using enums for UI elements
VehicleType with values CAR, BIKE, and TRUCK?Solution
Step 1: Recall enum declaration syntax
Standard enum syntax uses curly braces with comma-separated values.Step 2: Match syntax to options
enum VehicleType { CAR, BIKE, TRUCK } matches the correct syntax:enum VehicleType { CAR, BIKE, TRUCK }.Final Answer:
enum VehicleType { CAR, BIKE, TRUCK } -> Option BQuick Check:
Enum syntax = curly braces + commas [OK]
- Using equal sign instead of braces
- Using square brackets for enums
- Missing commas between values
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);Solution
Step 1: Identify vehicle type value
VehicleType is set to CAR.Step 2: Follow switch-case logic
For CAR, spot is assigned COMPACT.Final Answer:
COMPACT -> Option CQuick Check:
CAR maps to COMPACT spot [OK]
- Confusing BIKE case with CAR
- Assuming default runs for CAR
- Ignoring break statements
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?Solution
Step 1: Analyze if-else conditions
Only CAR and BIKE cases are handled explicitly.Step 2: Consider TRUCK input
TRUCK does not match any condition, so no print occurs.Final Answer:
No output because TRUCK case is not handled -> Option DQuick Check:
Unhandled enum value = no output [OK]
- Expecting syntax or runtime error
- Assuming default print happens
- Confusing enum invalidity
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?
Solution
Step 1: Understand enum benefits for mapping
Enums provide fixed sets, so mapping ensures clear, valid assignments.Step 2: Evaluate design options
Mapping dictionary with enums enforces rules and prevents invalid spots.Final Answer:
Use a mapping dictionary from VehicleType to SpotType and validate assignments -> Option AQuick Check:
Mapping enums = safe, clear assignments [OK]
- Using strings loses type safety
- Random assignment causes errors
- If-else without enums is error-prone
