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
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
Complete source code
Makefile
What each file does
- src/main.cpp
- Size and time helpers,
fee, theSpot/Tickettypes, theParkingLotclass and the command loop. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
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.txtwithtests/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
- 1Add electric-vehicle spots with charging fees.
- 2Let queued vehicles give up after 30 minutes.
- 3Print a visual map of the lot.
Built and tested it yourself? Mark it to update your dashboard.