AlgoViz

Design a car rental

hard 90 min

Design 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

0:00 / 4:48

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

91011121314151617
AAA
BBB
CCCC
DDDD

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

deskcallerreadyQueue<Car>FAMILYnoneVANV1booking 8BookingstatebookedsizeVANusesVANcarnonecalendarCalendarVAN 9-121VAN 9-180

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

    critical

    It answers about now. The customer asks about June.

    Wrong
    if car.is_available:    car.is_available = False
    Right
    if calendar.free(branch, size, period, now) > 0:    calendar.add(booking)
  • Counting overlaps

    major

    Bookings one after the other need one car. Counting all of them refuses good customers.

    Wrong
    used = sum(p.overlaps(wish) for p in busy)
    Right
    used = max(sum(p.start <= m < p.end for p in busy)           for m in moments)
  • Asking outside the lock

    critical

    Two customers count one and both add.

    Wrong
    if service.available(...)[size] > 0:    service.add(booking)
    Right
    with self._lock:    if self._calendar.free(...) <= 0: raise SoldOut    self._calendar.add(b)
  • Upgrading from the yard alone

    critical

    The van stands here and is promised at noon.

    Wrong
    if ready[VAN]: return ready[VAN].popleft()
    Right
    if ready[VAN] and calendar.free(branch, VAN, rest, now) > 0:    move the promise, then popleft
  • Money before the return

    major

    A declined card leaves a car in the yard that the system believes to be away.

    Wrong
    payments.charge(...)b.state = State.RETURNED
    Right
    b.state = State.RETURNED...payments.charge(...)      # later, with a key
  • Forgetting the cleaning after the wish

    minor

    The wish ends at 17:00 and the next use begins at 17:00. Nobody has cleaned.

    Wrong
    wish = period
    Right
    wish = 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.