Skip to content
BytePatterns

Designing a Parking Lot

Low-Level Design: lesson 10 of 10

The classic interview warm-up, answered in objects rather than adjectives.

Lesson 10 of 10 · 6 min

Designing a Parking Lot

Step 1 of 13

Name the objects before the classes: Lot, Spot, Vehicle, Ticket, and a pricing rule.

The Idea

Name the objects first: Lot, Spot, Vehicle, Ticket, and a pricing rule. Give each one job — the spot knows its size, the ticket knows when entry happened, the lot only matches and counts. Then make allocation and pricing pluggable, because those are the parts that change.

Real-World Example

A ferry loading deck. Vehicles are graded by height and length at the ramp, sent to the lane that fits, and handed a card recording where they went. On arrival the card, not the driver's memory, decides which lane empties first.

The Code

class Lot:
    def __init__(self, counts): self.free = dict(counts); self.tickets = {}
    def park(self, plate, size):
        if self.free.get(size, 0) == 0: return None   # that class is full
        self.free[size] -= 1
        self.tickets[plate] = size
        return plate + "-" + size                     # the ticket
    def leave(self, plate):
        size = self.tickets.pop(plate)
        self.free[size] += 1
        return "freed " + size
lot = Lot({"small": 1, "large": 0})
print(lot.park("AB12", "small"))     # AB12-small
print(lot.park("CD34", "small"))     # None
print(lot.leave("AB12"), lot.free)   # freed small {'small': 1, 'large': 0}

Your turn

Fill in the blank.

class Lot:
  def __init__(self, n): self.free = n
  def park(self):
      if self.free == 0: return None
      ___
      return "ok"

lot = Lot(2)
print(lot.park(), lot.park(), lot.free)   # should print ok ok 0

Mini quiz

1 / 3

The first thing to nail down is: