Design a Parking Lot System
Build an in-memory parking lot management system that assigns vehicles to appropriate slots, generates parking tickets, handles vehicle exit, and provides real-time parking availability.
Design and implement a parking lot management system that can manage multiple floors and parking slots while efficiently assigning available parking spaces to incoming vehicles.
The system should support different types of vehicles and ensure that a vehicle is parked only in a compatible slot.
For example, consider a parking lot with 2 floors, where every floor contains 6 slots. Each floor follows this layout:
Slot 1 → Truck
Slot 2 → Bike
Slot 3 → Bike
Slot 4 → Car
Slot 5 → Car
Slot 6 → Car
When a car arrives, the system should find the first available car slot by checking floors from the lowest floor to the highest floor. If Floor 1 has an available slot 4, the car should be parked there instead of using a slot on Floor 2. Once the vehicle is parked, the system should generate a ticket identifying its exact location.
This one tests whether you can model floor and slot allocation cleanly — type-compatible slot search with floor-then-slot priority, ticket generation, and real-time availability queries — while keeping the design open to dynamic layouts, multiple lots, and pricing.
Objective
The primary objective is to design and implement an in-memory parking lot system that supports configurable floors and slots per floor, type-compatible parking with first-available strategy, ticket-based unparking, and real-time display of free and occupied slots.
Functional Requirements
Requirements are split into three tiers so you know what to prioritize under time pressure: build first, build if time allows, and handle edge cases correctly.
Part 1 — Basic Requirements
Core functionality you must build and get working first.
1. Parking Lot
Example:
create_parking_lot PR1234 2 6
Creates:
Floor 1 → 6 slots
Floor 2 → 6 slots
2. Parking Floors
For example, with 6 slots:
Floor 1
1 2 3 4 5 6
Truck Bike Bike Car Car Car
The same layout is repeated for every floor. Slots on each floor are numbered sequentially starting from 1.
3. Vehicles
Every vehicle has:
- Vehicle type
- Registration number
- Color
Example:
Type: CAR
Registration: KA-01-DB-1234
Color: black
4. Parking a Vehicle
A slot is suitable only when its configured vehicle type matches the incoming vehicle — a CAR cannot be parked in a BIKE slot.
5. Finding the Parking Slot
For example, suppose:
Floor 1: Slot 4 → Occupied, Slot 5 → Free, Slot 6 → Free
Floor 2: Slot 4 → Free
A new car must be assigned Floor 1, Slot 5 — it should not jump to Floor 2.
6. Parking Ticket
7. Unparking a Vehicle
Example:
unpark_vehicle PR1234_2_5
If the ticket is invalid or the slot is already empty:
Invalid Ticket
8. Display Parking Information
Examples:
display free_count CAR
→ No. of free slots for CAR on Floor 1: 3
No. of free slots for CAR on Floor 2: 2
display free_slots BIKE
→ Free slots for BIKE on Floor 1: 2,3
Free slots for BIKE on Floor 2: 2
display occupied_slots CAR
→ Occupied slots for CAR on Floor 1: 4,5
Occupied slots for CAR on Floor 2: 6
Part 2 — Bonus Features
Extensibility checks — can your design support dynamic layouts, multiple lots, and history/pricing without a rewrite.
Part 3 — Validations & Edge Cases
1. Slot Compatibility
A bike cannot occupy a car slot even if the car slot is empty.
2. Slot Numbering
Slots on each floor start from 1 and restart for every floor — Floor 1 → 1,2,3,4,5,6 and Floor 2 → 1,2,3,4,5,6.
3. Floor & Slot Priority
Floors are searched in ascending order; within a floor, slots are scanned in ascending order. The first compatible free slot wins.
4. Parking Lot Full
If no compatible slot is available anywhere, print:
Parking Lot Full
This is per vehicle type — the lot may have free bike slots while car slots are full.
park_vehicle CAR KA-01-DF-8230 black
→ Parking Lot Full
5. Ticket Validation
A ticket is valid only when the parking lot ID is valid, the floor exists, the slot exists, and the slot is currently occupied. Otherwise print Invalid Ticket.
6. Ranges and Configuration
create_parking_lot PR1234 3 10
→ 3 floors, 10 slots per floor (1→Truck, 2→Bike, 3→Bike, 4-10→Car)
7. Input Format
Examples:
create_parking_lot PR1234 2 6
park_vehicle CAR KA-01-DB-1234 black
park_vehicle BIKE KA-01-DB-1541 black
park_vehicle TRUCK KA-32-SJ-5389 orange
unpark_vehicle PR1234_2_5
display free_count CAR
display free_slots BIKE
display occupied_slots TRUCK
Example Usage: Full Walkthrough
1. Create the Parking Lot
> create_parking_lot PR1234 2 6
Created parking lot with 2 floors and 6 slots per floor
Floor 1: 1→Truck, 2→Bike, 3→Bike, 4→Car, 5→Car, 6→Car
Floor 2: 1→Truck, 2→Bike, 3→Bike, 4→Car, 5→Car, 6→Car
2. Check Initial Car Availability
> display free_count CAR
No. of free slots for CAR on Floor 1: 3
No. of free slots for CAR on Floor 2: 3
3. Park Cars
> park_vehicle CAR KA-01-DB-1234 black
Parked vehicle. Ticket ID: PR1234_1_4
> park_vehicle CAR KA-02-CB-1334 red
Parked vehicle. Ticket ID: PR1234_1_5
> park_vehicle CAR KA-01-DB-1133 black
Parked vehicle. Ticket ID: PR1234_1_6
> park_vehicle CAR KA-05-HJ-8432 white
Parked vehicle. Ticket ID: PR1234_2_4
4. Display Free Car Slots
> display free_slots CAR
Free slots for CAR on Floor 1:
Free slots for CAR on Floor 2: 5,6
5. Remove a Vehicle
> unpark_vehicle PR1234_2_5
Unparked vehicle with Registration Number: WB-45-HO-9032 and Color: white
Slot 5 on Floor 2 is now available.
6. Park Another Car
> park_vehicle CAR KA-21-HS-2347 red
Parked vehicle. Ticket ID: PR1234_2_5
System checks Floor 1 → No free car slot, Floor 2 → Slot 5 available → assigns PR1234_2_5.
7. Park a Bike
> park_vehicle BIKE KA-01-DB-1541 black
Parked vehicle. Ticket ID: PR1234_1_2
8. Park a Truck
> park_vehicle TRUCK KA-32-SJ-5389 orange
Parked vehicle. Ticket ID: PR1234_1_1
> park_vehicle TRUCK KL-12-HF-4542 green
Parking Lot Full
9. Display Occupied Slots
> display occupied_slots CAR
Occupied slots for CAR on Floor 1: 4,5,6
Occupied slots for CAR on Floor 2: 4,5,6
> display occupied_slots BIKE
Occupied slots for BIKE on Floor 1: 2
Occupied slots for BIKE on Floor 2:
10. Invalid Ticket
> unpark_vehicle PR1234_2_5
Invalid Ticket
The final system creates a parking lot, manages floors and vehicle-specific slots, assigns the first suitable slot, generates tickets, releases slots on exit, and displays real-time availability for cars, bikes, and trucks.