Design a parking lot
medium 90 minModel a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.
Watch lesson4:48
Check yourself
One question at a time. Each answer explains why.
Question 1 of 12
The business wants a new 'first 15 minutes free' rule. In a good design, what changes?
Your code does `if (spot.isFree()) { spot.occupy(); }` and two gates run it at once. What can happen, and what fixes it?
Why is a class ParkingSpot that only manages one space a good example of single responsibility?
Someone models canFitInSpot() as an abstract method that each vehicle implements. Which idea is that?
What is the best reason to have an abstract Vehicle class, if you have one at all?
A card is declined in leave(). Which state is right?
A car enters at 09:00 and leaves at 11:10. Rate 200 cents for each started hour. The bill?
Semaphores: a car takes a large permit because medium is out. On exit the code releases a permit for the car's own size. What breaks?
Signs are notified outside the lock. Which new problem must be solved?
Exit: the card request times out. What is the safe next step?
Why does size come before distance when choosing a space?
A stress test starts 8 threads for 8 free spaces and is always green. What is the first change?
Pick an answer to see why.
Not quite. With pricing behind an interface, a new rule is a new implementation, here a wrapper around the old one. The classes that use it stay closed.
Not quite. Both threads can pass the check before either occupies the space. An atomic claim closes the gap.
Not quite. Single responsibility is about reasons to change. A tariff or a search rule does not touch it.
Not quite. A call through a Vehicle reference runs the subclass version. It is correct, and here an enum field does the same with less.
Not quite. Shared state (plate, size) and getters. If nothing but one value differs, an enum is simpler.
Not quite. The car is still here and must be able to try again. Releasing first would sell a space with a car in it; losing the ticket would trap the car.
Not quite. 130 minutes are 7800 seconds. (7800 + 3599) // 3600 = 3 started hours, 600 cents.
Not quite. The permit must be the one that was taken. Keep the size of the space on the ticket.
Not quite. Two threads can overtake each other between unlock and notify. A version number lets a sign drop the older message.
Not quite. You do not know whether it worked. The same key makes the provider answer for the first request instead of charging again.
Not quite. Large spaces are the only ones a bus can use. A car has alternatives.
Not quite. With a space for everybody nobody competes. Fewer spaces and a barrier make them meet.
Flashcards
Say your answer, then reveal it.
Card 1 of 13
Why use the Strategy pattern for parking fees?
Prices change for their own reasons. Each rule is a class behind one interface, so a new rule never edits ParkingLot (open/closed).
Say your answer, then reveal it.
How do you stop two gates assigning the same space?
Make finding and taking one step: a short lock, an atomic poll from a free queue, compare and set, or a conditional update that checks the row count.
Say your answer, then reveal it.
Enum or subclass for vehicle types?
Enum with data when types differ only in facts (which sizes fit). Subclasses only when behaviour really differs.
Say your answer, then reveal it.
When does a level become a class?
When it owns something: a lock, its own free spaces, a sign. Until then it is a number on the space.
Say your answer, then reveal it.
Why is there no isFree flag on a space?
Being in the pool's queue is being free. A flag would be a second answer to the same question.
Say your answer, then reveal it.
Order of steps at the exit?
Take the ticket out, price, charge (no lock), release, open. On failure put the ticket back.
Say your answer, then reveal it.
What does enter return when the lot is full?
Nothing (None, Optional.empty). Full is an ordinary result. No ticket number is used.
Say your answer, then reveal it.
Is one lock enough for ten gates?
1.7 claims a second at 5 microseconds each is 0.00085% busy. Yes, as long as nothing slow happens inside.
Say your answer, then reveal it.
How do you round started hours without floats?
max(1, (seconds + 3599) // 3600).
Say your answer, then reveal it.
How do you bill a night with a clock change?
Length from UTC instants. Rate from the local time at the start of each hour.
Say your answer, then reveal it.
What is the right idempotency key for a payment?
Ticket id plus bill number. A retry has the same key and the same stored amount; a new bill has a new one.
Say your answer, then reveal it.
Semaphore: which order on release?
The space back into the queue first, then the permit. Otherwise a waiting driver finds an empty queue.
Say your answer, then reveal it.
Reserve a space or capacity?
Capacity. Choose the space on arrival, so one overstaying car breaks nobody's promise.
Say your answer, then reveal it.

These cards come back in Today, just before you would forget them.