Skip to content
C++ project #14Intermediate

Parking Lot System

Assign vehicles to the smallest suitable spot, queue them when the lot is full, charge by started hour with a free period and a daily cap, and report revenue.

Estimated time
~4 hours to build
Files
5 files · 385 lines
Technologies
C++17, <map>, <deque>, <optional>, <array>

Introduction

Parking lots are a favourite design-interview problem because they combine data modelling, allocation rules and money. Here bikes, cars and vans fit spots of their size or larger, overflow vehicles wait in a queue, and fees follow realistic rules.

Problem statement

Simulate a lot with 2 bike, 3 car and 1 van spots. enter parks a vehicle in the smallest free spot that fits or adds it to a waiting queue; exit computes the stay and fee (first 15 minutes free, then per started hour at bike $1 / car $2.50 / van $4, capped at 8 hours), frees the spot and gives it to the first queued vehicle that fits. Support finding a vehicle, showing spot status and a revenue report, and reject duplicates, bad times and unknown vehicle types.

Objectives

  • Model categories with enum class and ordered comparison
  • Use std::deque as a queue you can also search
  • Keep stable pointers into a vector that never resizes
  • Express pricing rules as a small pure function

Features

  • Smallest-fit allocation (a car may use a van spot)
  • Waiting queue served on exit, by fit
  • Fee rules with free period and cap
  • Status, find and revenue report

Concepts demonstrated

  • enum class
  • std::deque
  • std::optional
  • Allocation strategies
  • Pure functions

System architecture

ParkingLot builds its spots once in the constructor, so Spot* pointers stored in parked_ (plate → ticket and spot) stay valid. free_spot returns the first free spot whose size is ≥ the vehicle's size; spots are ordered bike, car, van, so this is the smallest fit. exit validates the time, computes the fee with fee(), frees the spot and scans the std::deque queue for the first vehicle that fits it. Times are parsed into minutes with std::optional returns for invalid input.

Folder structure

TextProject tree
parking-lot-system/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Size and time helpers, fee, the Spot/Ticket types, the ParkingLot class and the command loop.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Parking Lot === 2 bike, 3 car, 1 van spots. Rates/hour: bike $1, car $2.50, van $4 (first 15 min free)
> KA01AB1234 (car) parked in C1 at 08:05
> KA02CD5678 (car) parked in C2 at 08:20
> KA03EF0001 (bike) parked in B1 at 08:30
> KA04GH4321 (car) parked in C3 at 08:45
> KA05IJ8765 (car) parked in V1 at 09:00
> Lot full for a van; KA06KL1111 is #1 in the queue
> Usage: enter <bike|car|van> <plate> <HH:MM>
> KA01AB1234 is already here
> [B1 bike] KA03EF0001
[B2 bike] free
[C1 car ] KA01AB1234
[C2 car ] KA02CD5678
[C3 car ] KA04GH4321
[V1 van ] KA05IJ8765
queue: KA06KL1111(van)
> KA03EF0001 leaves B1 after 0h 10m: free
> KA01AB1234 leaves C1 after 3h 25m: $10.00
> KA05IJ8765 leaves V1 after 3h 10m: $10.00
queue: KA06KL1111 (van) parked in V1 at 12:10
> KA06KL1111 is in spot V1 since 12:10
> Usage: exit <plate> <HH:MM>
> KA02CD5678 leaves C2 after 10h 0m: $20.00
> KA08MN2222 (bike) parked in B1 at 13:00
> KA08MN2222 leaves B1 after 1h 5m: $2.00
> KA06KL1111 leaves V1 after 3h 30m: $16.00
> [B1 bike] free
[B2 bike] free
[C1 car ] free
[C2 car ] free
[C3 car ] KA04GH4321
[V1 van ] free
> KA99ZZ0000 is not parked here
> Occupied 1/6, waiting 0, completed stays 6, revenue $58.00
>
Input
enter car KA01AB1234 08:05
enter car KA02CD5678 08:20
enter bike KA03EF0001 08:30
enter car KA04GH4321 08:45
enter car KA05IJ8765 09:00
enter van KA06KL1111 09:10
enter truck KA07 09:15
enter car KA01AB1234 09:20
status
exit KA03EF0001 08:40
exit KA01AB1234 11:30
exit KA05IJ8765 12:10
find KA06KL1111
exit KA02CD5678 25:00
exit KA02CD5678 18:20
enter bike KA08MN2222 13:00
exit KA08MN2222 14:05
exit KA06KL1111 15:40
status
exit KA99ZZ0000 12:00
report
quit

Testing

  • Compare ./app < tests/sample-input.txt with tests/expected-output.txt.
  • When car spots are full, the fourth car takes the van spot and the van has to queue.
  • When that car leaves at 12:10, the van is parked from the queue immediately.
  • A 10-hour car stay costs the $20 cap; a 10-minute bike stay is free.

Common errors

Crash after more spots are added at run time

Pointers into a std::vector are invalidated when it grows; build spots once, or store indices instead.

One hour and one minute is charged as one hour

Round up to started hours: (minutes + 59) / 60.

Possible improvements

  • Multiple levels with per-level reports
  • Monthly passes that are free during the day
  • Persist the state to resume after a restart

Additional challenges

  1. 1Add electric-vehicle spots with charging fees.
  2. 2Let queued vehicles give up after 30 minutes.
  3. 3Print a visual map of the lot.

Built and tested it yourself? Mark it to update your dashboard.