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
(1) (4) (4) (2) (2) (2) ← capacity per spot
The same layout is repeated for every floor. Slots on each floor are numbered sequentially starting from 1. A single spot is homogeneous — it holds only one category at a time (either 1-4 bikes OR 1-2 cars OR 1 truck); a bike and a car never share the same spot.
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. The spot must also have capacity left and remain homogeneous (a bike spot with 2 bikes can take 2 more bikes, but not a car).
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
display registration_numbers_for_cars_with_colour black
→ KA-01-DB-1234, KA-01-DB-1133
display slot_number_for_registration_number KA-01-DB-1234
→ 4
display slot_numbers_for_cars_with_colour black
→ 4,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
display registration_numbers_for_cars_with_colour black
display slot_number_for_registration_number KA-01-DB-1234
display slot_numbers_for_cars_with_colour black
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
11. Query by colour and registration
> display registration_numbers_for_cars_with_colour black
KA-01-DB-1234, KA-01-DB-1133
> display slot_numbers_for_cars_with_colour black
4,6
> display slot_number_for_registration_number KA-01-DB-1234
4
> display slot_number_for_registration_number KA-99-XX-0000
Not found
12. Multi-capacity homogeneous spot & nearest-entry
> park_vehicle BIKE KA-01-DB-9991 white
Parked vehicle. Ticket ID: PR1234_1_2 // shares Floor1 Slot2 with KA-01-DB-1541 (2/4)
> park_vehicle BIKE KA-01-DB-9992 white
Parked vehicle. Ticket ID: PR1234_1_2 // 3/4 in same spot
> park_vehicle CAR KA-01-DB-9993 white
Parked vehicle. Ticket ID: PR1234_2_6 // cannot join bike spot — new car spot, nearest to entry
13. Payment on exit (time × category)
> unpark_vehicle PR1234_1_2
Unparked vehicle with Registration Number: KA-01-DB-1541 and Color: white
Duration: 2 hrs — Fee: ₹20 (Bike ₹10/hr)
> unpark_vehicle PR1234_1_4
Unparked vehicle with Registration Number: KA-01-DB-1234 and Color: black
Duration: 3 hrs — Fee: ₹60 (Car ₹20/hr)
Rates are assumptions stated to the reviewer (e.g., Bike ₹10/hr, Car ₹20/hr, Truck/Bus ₹30/hr); the spot frees capacity but remains homogeneous until empty.
The final system creates a parking lot, manages floors and vehicle-specific slots, assigns the nearest suitable spot, generates tickets, handles 1-4 bikes / 1-2 cars / 1 truck per homogeneous spot, answers colour/registration queries, and displays real-time availability plus time-based fees for cars, bikes, and trucks.