Design a car rental
hard 90 minDesign the classes of a rental company: search by period, booking a size, handing out a car, upgrades, late returns and bills.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
The board counts, the yard hands out: calls travel, fields change, and the matching lines light up.
Operations
Count the peak
Two cars, four uses, a wish from 10 to 16. No cleaning time here.
Step 1 of 6
The board
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | |
|---|---|---|---|---|---|---|---|---|---|
| A | A | A | |||||||
| B | B | B | |||||||
| C | C | C | C | ||||||
| D | D | D | D |
A from 9 to 11, B from 11 to 13, C from 12 to 15, D from 15 to 18. All four overlap the wish.
Step 1: The board
def free(self, branch, size, period, now): wish = Period(period.start, period.end + self._turnaround) busy = [self._busy(b, now) for b in self._bookings[branch, size]] busy = [p for p in busy if p.overlaps(wish)] moments = [wish.start] + [p.start for p in busy if p.start > wish.start] peak = max(sum(p.start <= m < p.end for p in busy) for m in moments) return self._fleet[branch, size] - peak- Cost
- Uses in the window, squared
- Guarantee
- Exact
- Counting overlaps
- refuses too much
Look at the start of the wish and at the starts inside it.
In the real world
The same car rental doing real jobs. Pick one and step through it.
It stands in the yard. It is not free.
Step 1 of 4
9:00
A van is ready. Booking 8 was promised a van from 12:00.
Step 1: 9:00
Common mistakes
The bugs interviewers watch for, with the fix beside each.
A flag on the car
criticalIt answers about now. The customer asks about June.
Wrongif car.is_available: car.is_available = FalseRightif calendar.free(branch, size, period, now) > 0: calendar.add(booking)Counting overlaps
majorBookings one after the other need one car. Counting all of them refuses good customers.
Wrongused = sum(p.overlaps(wish) for p in busy)Rightused = max(sum(p.start <= m < p.end for p in busy) for m in moments)Asking outside the lock
criticalTwo customers count one and both add.
Wrongif service.available(...)[size] > 0: service.add(booking)Rightwith self._lock: if self._calendar.free(...) <= 0: raise SoldOut self._calendar.add(b)Upgrading from the yard alone
criticalThe van stands here and is promised at noon.
Wrongif ready[VAN]: return ready[VAN].popleft()Rightif ready[VAN] and calendar.free(branch, VAN, rest, now) > 0: move the promise, then popleftMoney before the return
majorA declined card leaves a car in the yard that the system believes to be away.
Wrongpayments.charge(...)b.state = State.RETURNEDRightb.state = State.RETURNED...payments.charge(...) # later, with a keyForgetting the cleaning after the wish
minorThe wish ends at 17:00 and the next use begins at 17:00. Nobody has cleaned.
Wrongwish = periodRightwish = Period(period.start, period.end + turnaround)
Interview tips
Open with
- A car, or a kind of car?
- Same branch?
- What lies between two rentals?
Say
- Two deals: promise and hand-over.
- Two truths: calendar and yard.
- A booking has zero or one car.
Prove it
- Touching periods.
- A late car.
- A van promised at noon.
Never
- isAvailable.
- 16:59:59.
- Rent computed twice.