AlgoViz

Design a parking lot

medium 90 min

Model a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.

Watch lesson4:48

Explain your choices

Answer first. Then compare your reasoning.

15 questions 0 got it 0 to review15 not marked

Defend

Choice, reason, cost, trigger

ChoiceReasonCostI would change it when
Enum for vehiclesThey differ by one valueNo behaviour per typeTypes act differently
No isFree on a spaceOne fact, one homeAsking one space needs a lookupSensors report per space
One lock0.00085% busyAll gates share itA profile says so
Pop, charge, put backNo lock during a cardA ticket is nowhere for secondsStatus in a database
Price by spaceThe sign shows prices for sizesA motorcycle moved up pays moreCustomers complain: one word
Counter for idsShort, sortedGuessableThe paper opens a barrier
Size before distanceBuses find roomLonger walks for carsNo buses come

The interviewer says: make it a Singleton.

Breaks"Yes, with getInstance"
How
Global access, private constructor.
Limit
Every class can reach it, tests share it, and it needs a thread-safe start.
Choose when
Only if they insist. Then write the holder version.
Works"No, Singletons are bad"
How
Refuse by rule.
Limit
A rule is not a reason. They asked for one.
Choose when
Not enough alone.
Strong"One instance yes, one global no"
How
Create it in main, pass it to the gates. Offer the safe Singleton if they still want it.
Limit
A longer constructor call.
Choose when
Say what breaks: the second car park, and the second test.

Scan, queue, heap, or database?

BreaksScan for every arrival
How
Walk over all spaces.
Limit
Slowest when full, and it needs a lock the whole time.
Choose when
A toy.
WorksQueues by size
How
O(1), no distance.
Limit
Any space, not the nearest.
Choose when
First answer.
StrongThe structure for the question they asked
How
Queue for any, heap for nearest, bits for runs, conditional update for many machines.
Limit
More than one structure must be kept equal.
Choose when
After they say which one matters.