parking.md
· 2.4 KiB · Markdown
Raw
# Object-Oriented Design Interview: Parking Lot
"Design a parking lot" is the most common object-oriented design (OOD) question. The interviewer is checking whether you can turn vague requirements into clean classes and responsibilities, not whether you know a single right answer.
## Clarify first
- Multiple floors? Spot sizes (motorcycle, compact, large, EV charging)?
- Entry and exit gates, tickets, payment (hourly rate, flat fee)?
- Should it show free spots per floor on a display board?
## Core classes
```python
from enum import Enum
from datetime import datetime
class Size(Enum):
SMALL = 1; MEDIUM = 2; LARGE = 3
class Vehicle:
def __init__(self, plate, size): self.plate, self.size = plate, size
class Spot:
def __init__(self, spot_id, size):
self.id, self.size, self.vehicle = spot_id, size, None
def fits(self, v): return self.vehicle is None and v.size.value <= self.size.value
class Ticket:
def __init__(self, vehicle, spot):
self.vehicle, self.spot, self.entered = vehicle, spot, datetime.now()
class ParkingLot:
def __init__(self, floors): self.floors, self.active = floors, {}
def park(self, v):
for floor in self.floors:
for spot in floor:
if spot.fits(v):
spot.vehicle = v
t = Ticket(v, spot); self.active[v.plate] = t
return t
return None # lot full
def leave(self, plate, pricing):
t = self.active.pop(plate); t.spot.vehicle = None
return pricing.fee(t.entered, datetime.now())
```
## Design choices to talk about
- **Strategy pattern for pricing:** `HourlyPricing`, `FlatPricing`, weekend rates, all behind one `fee()` method.
- **Spot assignment strategy:** nearest to the entrance, or smallest spot that fits, behind an interface.
- **Faster lookup:** keep a free-spot set (or min-heap by distance) per size instead of scanning every spot.
- **Concurrency:** two gates can grab the same spot; lock the spot or use an atomic compare-and-set.
## What interviewers reward
Clear responsibilities (a ticket doesn't calculate fees), extensibility (adding EV spots touches one class), and asking about edge cases: lost tickets, full lot, a vehicle bigger than any spot.
---
*Disclosure: I'm part of the [Linkjob AI](https://www.linkjob.ai/) team; we build an AI interview assistant.*
Object-Oriented Design Interview: Parking Lot
"Design a parking lot" is the most common object-oriented design (OOD) question. The interviewer is checking whether you can turn vague requirements into clean classes and responsibilities, not whether you know a single right answer.
Clarify first
- Multiple floors? Spot sizes (motorcycle, compact, large, EV charging)?
- Entry and exit gates, tickets, payment (hourly rate, flat fee)?
- Should it show free spots per floor on a display board?
Core classes
from enum import Enum
from datetime import datetime
class Size(Enum):
SMALL = 1; MEDIUM = 2; LARGE = 3
class Vehicle:
def __init__(self, plate, size): self.plate, self.size = plate, size
class Spot:
def __init__(self, spot_id, size):
self.id, self.size, self.vehicle = spot_id, size, None
def fits(self, v): return self.vehicle is None and v.size.value <= self.size.value
class Ticket:
def __init__(self, vehicle, spot):
self.vehicle, self.spot, self.entered = vehicle, spot, datetime.now()
class ParkingLot:
def __init__(self, floors): self.floors, self.active = floors, {}
def park(self, v):
for floor in self.floors:
for spot in floor:
if spot.fits(v):
spot.vehicle = v
t = Ticket(v, spot); self.active[v.plate] = t
return t
return None # lot full
def leave(self, plate, pricing):
t = self.active.pop(plate); t.spot.vehicle = None
return pricing.fee(t.entered, datetime.now())
Design choices to talk about
- Strategy pattern for pricing:
HourlyPricing,FlatPricing, weekend rates, all behind onefee()method. - Spot assignment strategy: nearest to the entrance, or smallest spot that fits, behind an interface.
- Faster lookup: keep a free-spot set (or min-heap by distance) per size instead of scanning every spot.
- Concurrency: two gates can grab the same spot; lock the spot or use an atomic compare-and-set.
What interviewers reward
Clear responsibilities (a ticket doesn't calculate fees), extensibility (adding EV spots touches one class), and asking about edge cases: lost tickets, full lot, a vehicle bigger than any spot.
Disclosure: I'm part of the Linkjob AI team; we build an AI interview assistant.