linkjobai revised this gist 1 day ago. Go to revision
1 file changed, 59 insertions
parking.md(file created)
| @@ -0,0 +1,59 @@ | |||
| 1 | + | # Object-Oriented Design Interview: Parking Lot | |
| 2 | + | ||
| 3 | + | "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. | |
| 4 | + | ||
| 5 | + | ## Clarify first | |
| 6 | + | ||
| 7 | + | - Multiple floors? Spot sizes (motorcycle, compact, large, EV charging)? | |
| 8 | + | - Entry and exit gates, tickets, payment (hourly rate, flat fee)? | |
| 9 | + | - Should it show free spots per floor on a display board? | |
| 10 | + | ||
| 11 | + | ## Core classes | |
| 12 | + | ||
| 13 | + | ```python | |
| 14 | + | from enum import Enum | |
| 15 | + | from datetime import datetime | |
| 16 | + | ||
| 17 | + | class Size(Enum): | |
| 18 | + | SMALL = 1; MEDIUM = 2; LARGE = 3 | |
| 19 | + | ||
| 20 | + | class Vehicle: | |
| 21 | + | def __init__(self, plate, size): self.plate, self.size = plate, size | |
| 22 | + | ||
| 23 | + | class Spot: | |
| 24 | + | def __init__(self, spot_id, size): | |
| 25 | + | self.id, self.size, self.vehicle = spot_id, size, None | |
| 26 | + | def fits(self, v): return self.vehicle is None and v.size.value <= self.size.value | |
| 27 | + | ||
| 28 | + | class Ticket: | |
| 29 | + | def __init__(self, vehicle, spot): | |
| 30 | + | self.vehicle, self.spot, self.entered = vehicle, spot, datetime.now() | |
| 31 | + | ||
| 32 | + | class ParkingLot: | |
| 33 | + | def __init__(self, floors): self.floors, self.active = floors, {} | |
| 34 | + | def park(self, v): | |
| 35 | + | for floor in self.floors: | |
| 36 | + | for spot in floor: | |
| 37 | + | if spot.fits(v): | |
| 38 | + | spot.vehicle = v | |
| 39 | + | t = Ticket(v, spot); self.active[v.plate] = t | |
| 40 | + | return t | |
| 41 | + | return None # lot full | |
| 42 | + | def leave(self, plate, pricing): | |
| 43 | + | t = self.active.pop(plate); t.spot.vehicle = None | |
| 44 | + | return pricing.fee(t.entered, datetime.now()) | |
| 45 | + | ``` | |
| 46 | + | ||
| 47 | + | ## Design choices to talk about | |
| 48 | + | ||
| 49 | + | - **Strategy pattern for pricing:** `HourlyPricing`, `FlatPricing`, weekend rates, all behind one `fee()` method. | |
| 50 | + | - **Spot assignment strategy:** nearest to the entrance, or smallest spot that fits, behind an interface. | |
| 51 | + | - **Faster lookup:** keep a free-spot set (or min-heap by distance) per size instead of scanning every spot. | |
| 52 | + | - **Concurrency:** two gates can grab the same spot; lock the spot or use an atomic compare-and-set. | |
| 53 | + | ||
| 54 | + | ## What interviewers reward | |
| 55 | + | ||
| 56 | + | 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. | |
| 57 | + | ||
| 58 | + | --- | |
| 59 | + | *Disclosure: I'm part of the [Linkjob AI](https://www.linkjob.ai/) team; we build an AI interview assistant.* | |
Newer
Older