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.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
Objects at the gates: calls travel, fields change, and the matching lines light up.
Operations
A car enters
One call from the gate, four steps under one lock.
Step 1 of 6
At the barrier
The lot has five free spaces. The gate knows only the lot.
Step 1: At the barrier
def enter(self, vehicle): with self._lock: if vehicle.plate in self._inside: raise AlreadyInside(vehicle.plate) spot = self._pool.claim(vehicle.type.min_size) if spot is None: return None ticket = Ticket(next(self._ids), vehicle, spot, self._now()) self._inside.add(vehicle.plate) self._active[ticket.id] = ticket return ticket- Cost
- O(1)
- Guarantee
- One space, one ticket
- Scan of 500
- up to 500 looks
Nothing slow happens while the lock is held.
In the real world
The same parking lot doing real jobs. Pick one and step through it.
What is taken out must be put back.
Step 1 of 4
The ticket is out
The exit has popped ticket 1. For a moment it is in no list.
Step 1: The ticket is out
def leave(self, ticket_id): with self._lock: ticket = self._active.pop(ticket_id, None) if ticket is None: raise UnknownTicket(ticket_id) try: left_at = self._now() cents = self._pricing.price_cents(ticket, left_at) self._payments.charge(f"ticket-{ticket_id}", cents) except Exception: with self._lock: self._active[ticket_id] = ticket raise with self._lock: self._pool.release(ticket.spot) self._inside.discard(ticket.vehicle.plate) return Receipt(ticket, left_at, cents)Common mistakes
The bugs interviewers watch for, with the fix beside each.
Check, then act
criticalTwo gates pass the check before either acts. One space is sold twice.
Wrongif spot.is_free(): spot.park(car)Rightspot = pool.claim(car.type.min_size) # finds and takesif spot is None: show("FULL")Releasing before the charge
criticalThe card is declined, the car stays, and the space has been given to the next driver.
Wrongpool.release(ticket.spot)payments.charge(key, cents)Rightpayments.charge(key, cents)pool.release(ticket.spot)Charging under the lock
criticalOne slow card stops every entry and every exit.
Wrongwith self._lock: ticket = self._active.pop(ticket_id) self._payments.charge(key, cents)Rightwith self._lock: ticket = self._active.pop(ticket_id)self._payments.charge(key, cents) # outsideRounding down
major119 minutes become 1 hour. Nobody complains, so nobody finds it.
Wronghours = seconds // 3600Righthours = max(1, (seconds + 3599) // 3600)A subclass for each size
majorThe next property doubles the tree.
Wrongclass LargeElectricSpot(LargeSpot): ...RightSpot("E1", 1, SpotSize.LARGE, features={"charger"})Asking the system clock
minorA test cannot reach 23:59, or the night when clocks change.
Wrongleft_at = datetime.now()Rightleft_at = self._now() # passed in
Interview tips
Open with
- Two verbs: enter, leave.
- One owner of free spaces.
- One rule for prices.
Reject aloud
- A level is a number.
- A gate is a caller.
- A car is an enum value.
Prove it
- Trace a full lot.
- Trace a declined card.
- free + active = total.
Never
- A lock around a card.
- A float for money.
- isFull before enter.