{"id":"ood-parking-lot","title":"Design a parking lot","sections":[{"id":"overview","title":"Overview","html":"<p>Picture a three-storey garage next to a train station. Motorcycles, cars and the occasional bus drive in at the entrance. The garage has to decide where each one goes, refuse it when there's no room, and free the space again when it drives out. A bus is the awkward case: it's too long for one space, so it needs a stretch of large spaces side by side.</p>\n<p>\"Design a parking lot\" is an <strong>object-oriented design</strong> question. You aren't asked for servers or databases. You're asked to model a small world in code. A good answer has four parts: clarify the scope with a few questions, name the classes and what each one is responsible for, write the code, and then discuss how you'd extend it. The interviewer is listening for clean responsibilities and sensible trade-offs more than for clever algorithms.</p>","headings":[]},{"id":"constraints-and-assumptions","title":"Constraints and assumptions","html":"<h3 id=\"constraints-and-assumptions\">Constraints and assumptions</h3>\n<p>Ask these questions first. Each answer changes the classes you need.</p>\n<ul>\n<li>\n<p><strong>What types of vehicles should we support?</strong> Motorcycles, cars and buses. <em>Why it matters:</em> this fixes the vehicle classes, and three types with different rules is a hint that polymorphism will help.</p>\n</li>\n<li>\n<p><strong>Does each vehicle type take up a different amount of parking space?</strong> Yes. <em>Why it matters:</em> the lot needs spot sizes, and a rule for which vehicle may use which size. The rules we'll use:</p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>Spot size</th><th>Who can park there</th></tr></thead><tbody><tr><td>Motorcycle spot</td><td>Motorcycle</td></tr><tr><td>Compact spot</td><td>Motorcycle, car</td></tr><tr><td>Large spot</td><td>Motorcycle, car</td></tr><tr><td>Five consecutive large spots</td><td>Bus</td></tr></tbody></table></div>\n</li>\n<li>\n<p><strong>Can a bus park in a single spot?</strong> No. A bus can park only if there are 5 consecutive large spots free. <em>Why it matters:</em> parking stops being \"find one free spot\" and becomes \"find a run of adjacent free spots\", so the code needs to know which spots are next to each other.</p>\n</li>\n<li>\n<p><strong>Does the parking lot have multiple levels?</strong> Yes. <em>Why it matters:</em> the lot is made of levels, and the search for a space has to go level by level.</p>\n</li>\n</ul>\n<p>We add a few assumptions of our own and say them out loud:</p>\n<ul>\n<li>Each level is laid out in <strong>rows</strong>, and each row holds 10 spots in a line. \"Consecutive\" means next to each other in the same row on the same level. Two spots at the ends of different rows don't count, even if their numbers are adjacent.</li>\n<li>A vehicle is identified by its license plate, and the same plate can't be parked twice.</li>\n<li>One entrance handles one vehicle at a time. Concurrency, tickets and payment are extensions we discuss at the end.</li>\n</ul>","headings":[{"id":"constraints-and-assumptions","title":"Constraints and assumptions","depth":3}]},{"id":"class-design","title":"Class design","html":"<h3 id=\"class-design\">Class design</h3>\n<figure class=\"diagram\" data-text=\"ParkingLot -> Level (has many); Level -> Row (has many); Row -> ParkingSpot (has many); ParkingSpot -> SpotSize enum (has a); Vehicle (abstract) -> ParkingSpot (occupies); Motorcycle -> Vehicle (abstract); Car -> Vehicle (abstract); Bus 5 spots -> Vehicle (abstract)\"><div class=\"diagram-scroll\"><svg class=\"dg\" viewBox=\"0 -10 800 390\" width=\"800\" role=\"img\" aria-label=\"A lot is made of levels, levels of rows, rows of spots. Vehicles decide which spot sizes they fit.\"><defs><marker id=\"dg-arrow\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path class=\"dg-arrowhead\" d=\"M0,1 L9,5 L0,9 z\"></path></marker></defs><line class=\"dg-edge\" x1=\"151\" y1=\"42\" x2=\"167\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"311\" y1=\"42\" x2=\"327\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"471\" y1=\"42\" x2=\"487\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"631\" y1=\"42\" x2=\"647\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"560\" y1=\"193\" x2=\"560\" y2=\"71\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"436\" y1=\"315\" x2=\"518.7\" y2=\"253\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"560\" y1=\"315\" x2=\"560\" y2=\"253\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"681.3\" y1=\"313\" x2=\"601.3\" y2=\"253\" marker-end=\"url(#dg-arrow)\"></line><rect class=\"dg-node dg-box\" x=\"12\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"80\" y=\"46.5\">ParkingLot</tspan></text><rect class=\"dg-node dg-box\" x=\"172\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"240\" y=\"46.5\">Level</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"46.5\">Row</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"46.5\">ParkingSpot</tspan></text><rect class=\"dg-node dg-note\" x=\"652\" y=\"16\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label dg-muted\" text-anchor=\"middle\"><tspan x=\"720\" y=\"38.5\">SpotSize</tspan><tspan x=\"720\" y=\"54.5\">enum</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"196\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"218.5\">Vehicle</tspan><tspan x=\"560\" y=\"234.5\">(abstract)</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"318\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"346.5\">Motorcycle</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"318\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"346.5\">Car</tspan></text><rect class=\"dg-node dg-box\" x=\"652\" y=\"316\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"720\" y=\"338.5\">Bus</tspan><tspan x=\"720\" y=\"354.5\">5 spots</tspan></text><rect class=\"dg-edge-bg\" x=\"129.2\" y=\"-4\" width=\"59.6\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"159\" y=\"9\" text-anchor=\"middle\">has many</text><rect class=\"dg-edge-bg\" x=\"289.2\" y=\"-4\" width=\"59.6\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"319\" y=\"9\" text-anchor=\"middle\">has many</text><rect class=\"dg-edge-bg\" x=\"449.2\" y=\"-4\" width=\"59.6\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"479\" y=\"9\" text-anchor=\"middle\">has many</text><rect class=\"dg-edge-bg\" x=\"618.5\" y=\"-6\" width=\"41\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"639\" y=\"7\" text-anchor=\"middle\">has a</text><rect class=\"dg-edge-bg\" x=\"530.2\" y=\"123\" width=\"59.6\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"560\" y=\"136\" text-anchor=\"middle\">occupies</text></svg></div><figcaption>A lot is made of levels, levels of rows, rows of spots. Vehicles decide which spot sizes they fit.</figcaption></figure>\n<p>Each class has one job:</p>\n<ul>\n<li><strong><code>SpotSize</code></strong> is an <strong>enum</strong>, a fixed set of named values: <code>MOTORCYCLE</code>, <code>COMPACT</code> and <code>LARGE</code>. Using an enum instead of strings means a typo like <code>\"compcat\"</code> fails at once instead of quietly never matching. We number the sizes smallest to largest so that sorting them gives a \"try the smallest first\" order.</li>\n<li><strong><code>Vehicle</code></strong> is an <strong>abstract base class</strong>: a class that defines what every vehicle can do but can't be created on its own. It holds the license plate and the list of spots the vehicle currently holds. Its abstract method <code>fits_in(size)</code> answers \"can I use a spot of this size?\". A class attribute <code>spots_needed</code> says how many consecutive spots it takes, 1 by default.</li>\n<li><strong><code>Motorcycle</code>, <code>Car</code> and <code>Bus</code></strong> are subclasses that each answer <code>fits_in</code> their own way. A motorcycle fits anywhere. A car fits compact or large spots. A bus fits only large spots and sets <code>spots_needed = 5</code>. This is <strong>polymorphism</strong>: the level calls <code>vehicle.fits_in(size)</code> without checking which kind of vehicle it has, and each class supplies the right answer.</li>\n<li><strong><code>ParkingSpot</code></strong> knows its level, row, number and size, and which vehicle is in it, if any. It can be occupied and released, and it refuses to be occupied twice.</li>\n<li><strong><code>Row</code></strong> is an ordered list of spots. Its one job is to find a run of free, adjacent spots of a given size that is long enough for a vehicle.</li>\n<li><strong><code>Level</code></strong> owns its rows and keeps a count of free spots of each size. It decides where on this level a vehicle goes.</li>\n<li><strong><code>ParkingLot</code></strong> owns the levels, tries them in order, and remembers which vehicles are parked so it can free their spots when they leave.</li>\n</ul>\n<p>The lot, level, row and spot are joined by <strong>composition</strong>: each one is built out of the smaller ones and owns them. Vehicles and spots point at each other only while a vehicle is parked.</p>","headings":[{"id":"class-design","title":"Class design","depth":3}]},{"id":"solution","title":"Solution","html":"<h3 id=\"solution\">Solution</h3>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __future__</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> annotations</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> abc </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> ABC</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, abstractmethod</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> enum </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> IntEnum</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> SpotSize</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">IntEnum</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Spot sizes, smallest first, so sorting them gives a best-fit order.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    MOTORCYCLE</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    COMPACT</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 2</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    LARGE</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 3</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Vehicle</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">ABC</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    spots_needed </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  # how many consecutive spots the vehicle takes</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, license_plate: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.license_plate </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> license_plate</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.spots: list[ParkingSpot] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># spots it holds while parked</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">    @abstractmethod</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> fits_in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, size: SpotSize) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">        \"\"\"Can this vehicle use a spot of this size?\"\"\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __repr__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{type</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">).</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">__name__}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.license_plate</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">!r</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">)\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Motorcycle</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Vehicle</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> fits_in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, size: SpotSize) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> True</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  # any spot</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Car</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Vehicle</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> fits_in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, size: SpotSize) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (SpotSize.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">COMPACT</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, SpotSize.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">LARGE</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Bus</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Vehicle</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    spots_needed </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 5</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> fits_in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, size: SpotSize) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> SpotSize.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">LARGE</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> ParkingSpot</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, level: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, row: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, number: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, size: SpotSize) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.level </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> level</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.row </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> row</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.number </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> number</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.vehicle: Vehicle </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> is_free</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.vehicle </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> occupy</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, vehicle: Vehicle) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.is_free():</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            raise</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> ValueError</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"spot </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> is already taken\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.vehicle </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> release</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.vehicle </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __repr__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"L</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.level</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">-R</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.row</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">-#</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.number</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.size.name</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">)\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Row</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Spots in a row, in order. Only spots in the same row are consecutive.\"\"\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, spots: list[ParkingSpot]) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.spots </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spots</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> find_run</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, vehicle: Vehicle, size: SpotSize) -> list[ParkingSpot] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">        \"\"\"First run of free, adjacent spots of this size that is long enough.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        run: list[ParkingSpot] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.spots:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spot.is_free() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">and</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spot.size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                run.append(spot)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                if</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(run) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle.spots_needed:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> run</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            else</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                run </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># a taken or wrong-size spot breaks the run</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Level</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, floor: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, row_layouts: list[list[SpotSize]]) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.floor </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> floor</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.rows </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            Row([ParkingSpot(floor, r, n, size) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> n, size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> enumerate</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(layout)])</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> r, layout </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> enumerate</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(row_layouts)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        ]</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.free_by_size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {size: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">0</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> SpotSize}</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> row </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.rows:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> row.spots:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">                self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.free_by_size[spot.size] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> park</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, vehicle: Vehicle) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#6A737D;--shiki-dark:#6A737D\">        # Best fit: try the smallest spot size the vehicle accepts first.</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> sorted</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(s </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> s </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> SpotSize </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle.fits_in(s)):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.free_by_size[size] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle.spots_needed:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                continue</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  # quick skip: not enough spots of this size left</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> row </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.rows:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                run </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> row.find_run(vehicle, size)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> run:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                    for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> run:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                        spot.occupy(vehicle)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                    vehicle.spots </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> run</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">                    self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.free_by_size[size] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">-=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(run)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                    return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> True</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> False</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> spot_freed</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, spot: ParkingSpot) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.free_by_size[spot.size] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> ParkingLot</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, levels: list[Level]) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.levels </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> levels</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.levels_by_floor </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {level.floor: level </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> level </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> levels}</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.parked: dict[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, Vehicle] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {}  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># license plate -> vehicle</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> park</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, vehicle: Vehicle) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle.license_plate </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.parked:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            raise</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> ValueError</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">vehicle</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> is already parked\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> level </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.levels:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> level.park(vehicle):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">                self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.parked[vehicle.license_plate] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> True</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> False</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  # lot full for this kind of vehicle</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> leave</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, license_plate: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) -> Vehicle:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        vehicle </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.parked.pop(license_plate)  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># KeyError if not parked</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> spot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle.spots:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            spot.release()</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">            self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.levels_by_floor[spot.level].spot_freed(spot)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        vehicle.spots </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> vehicle</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> free_spots</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> dict[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, dict[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">]]:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            level.floor: {size.name: n </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> size, n </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> level.free_by_size.items()}</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> level </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.levels</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        }</span></span></code></pre>\n<p>The key parts:</p>\n<ul>\n<li><strong>The fit rule lives in the vehicle.</strong> <code>Level.park</code> asks <code>vehicle.fits_in(size)</code> and never checks <code>isinstance(vehicle, Bus)</code>. Adding a new vehicle type, such as a van that needs two compact spots, means writing one subclass. No other class changes.</li>\n<li><strong>Best fit.</strong> <code>Level.park</code> tries the spot sizes the vehicle accepts from smallest to largest. A motorcycle takes a motorcycle spot if one is free, then a compact one, and only then a large one. This keeps large spots free for the buses that can't use anything else.</li>\n<li><strong>Consecutive spots.</strong> <code>Row.find_run</code> walks the row in order and builds a run of free spots of the right size. A taken spot, or a spot of the wrong size, resets the run to empty. As soon as the run reaches <code>spots_needed</code>, it returns those spots. A car or a motorcycle needs a run of 1, so the same code handles every vehicle.</li>\n<li><strong>Free counts that go both ways.</strong> Each level keeps <code>free_by_size</code>, counted from its layout when it's built. Parking subtracts the spots taken and <code>spot_freed</code> adds one back for each spot released. The counts let a level skip a size at once when it can't possibly have enough spots, and they power an availability display. A count that only ever goes up, or that starts at 0, would be wrong after the first car.</li>\n<li><strong>Leaving.</strong> <code>ParkingLot.leave</code> looks up the vehicle by plate, releases every spot it holds (five for a bus), tells each spot's level, and clears the vehicle's list. Removing the vehicle from <code>parked</code> first means a second <code>leave</code> with the same plate raises a <code>KeyError</code> instead of freeing spots twice.</li>\n<li><strong>Guard rails.</strong> <code>ParkingSpot.occupy</code> raises if the spot is taken, and <code>ParkingLot.park</code> raises if the plate is already parked. Bugs like these are much easier to find when they fail loudly.</li>\n</ul>","headings":[{"id":"solution","title":"Solution","depth":3}]},{"id":"walking-through-an-example","title":"Walking through an example","html":"<h3 id=\"walking-through-an-example\">Walking through an example</h3>\n<p>Build a lot with two levels. Level 0 has two rows of 10 spots. Row 0 is two motorcycle spots, three compact and five large. Row 1 is five compact, four large and one compact at the end. Level 1 has one row of 10 large spots.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parking_lot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> *</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">M, C, L </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> SpotSize.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">MOTORCYCLE</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, SpotSize.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">COMPACT</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, SpotSize.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">LARGE</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">lot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ParkingLot([</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    Level(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, [[M, M, C, C, C, L, L, L, L, L],</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">              [C, C, C, C, C, L, L, L, L, C]]),</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    Level(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, [[L] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 10</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">]),</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">])</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> v </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [Motorcycle(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"MOTO-1\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">), Car(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"CAR-1\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">), Bus(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"BUS-1\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">), Bus(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"BUS-2\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)]:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    print</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(v, lot.park(v), v.spots)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">lot.leave(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"BUS-1\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span></code></pre>\n<p>What happens, step by step:</p>\n<ol>\n<li><strong>The motorcycle</strong> arrives. Level 0 tries the smallest size first, <code>MOTORCYCLE</code>, and finds spot 0 in row 0. It's parked at <code>L0-R0-#0</code>.</li>\n<li><strong>The car</strong> can't use motorcycle spots, so level 0 starts at <code>COMPACT</code>. Spot 2 in row 0 is the first compact spot, so the car goes to <code>L0-R0-#2</code>.</li>\n<li><strong>The first bus</strong> needs five consecutive large spots. Row 0 has large spots 5 to 9, all free, so the run reaches five at spot 9. The bus holds <code>L0-R0-#5</code> to <code>#9</code>.</li>\n<li><strong>The second bus</strong> tries level 0 again. The level now has 4 large spots free, fewer than 5, so it skips the level without scanning a single row. That's the free count paying off. (Even scanning would fail: row 1's four large spots are boxed in by compact spots on both sides.) Level 1 has a run of ten, so the bus takes <code>L1-R0-#0</code> to <code>#4</code>.</li>\n<li><strong>The first bus leaves.</strong> All five spots are released, and level 0's large count goes from 4 back to 9.</li>\n</ol>\n<p>The free counts after each stage, as <code>lot.free_spots()</code> prints them:</p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>Moment</th><th>Level 0 (motorcycle, compact, large)</th><th>Level 1 (large)</th></tr></thead><tbody><tr><td>Empty lot</td><td>2, 9, 9</td><td>10</td></tr><tr><td>After all four park</td><td>1, 8, 4</td><td>5</td></tr><tr><td>After the first bus leaves</td><td>1, 8, 9</td><td>5</td></tr></tbody></table></div>\n<p>The tests for this chapter also check the edge cases: a car is refused when only motorcycle spots are left, a third motorcycle is refused by a lot with two motorcycle spots, a car parked in the middle of six large spots blocks a bus, and a motorcycle falls back to a compact spot and then a large one.</p>","headings":[{"id":"walking-through-an-example","title":"Walking through an example","depth":3}]},{"id":"design-choices-and-trade-offs","title":"Design choices and trade-offs","html":"<h3 id=\"design-choices-and-trade-offs\">Design choices and trade-offs</h3>\n<p><strong>Who decides whether a vehicle fits?</strong> We put the rule in the vehicle (<code>fits_in</code>). The alternative is to put it in the spot, with a table of sizes to allowed vehicle types. The vehicle-side rule makes new vehicle types cheap to add. A spot-side table makes new spot types cheap to add, such as a disabled-access spot that any vehicle with a permit can use. Pick the side that's more likely to change, and say which one you picked.</p>\n<p><strong>Where should the bus go?</strong> We search levels in order and take the first run that fits, with a best-fit preference for small spots within a level. That's simple and predictable. It isn't globally optimal: a motorcycle on level 0 may take a compact spot while a motorcycle spot is free on level 1. Fixing that means checking every level before choosing, which costs more work per arrival. A real garage usually prefers the simple rule, because drivers enter at the bottom anyway.</p>\n<p><strong>Fragmentation.</strong> Cars parked in large spots can break up the runs a bus needs, the same way small allocations fragment memory. Best fit reduces this by sending cars to compact spots first. A stricter rule would forbid cars from large spots while buses are expected, at the cost of turning cars away when compact spots run out.</p>\n<p><strong>Linear search.</strong> <code>Row.find_run</code> scans every spot, so parking costs time proportional to the number of spots. For a garage of a few thousand spots, that's a few thousand checks, which is nothing. For a lot that size the clarity is worth more than an index. If it ever mattered, a level could keep a set of free spots per size for single-spot vehicles and only scan rows for buses.</p>\n<p><strong>Two-way references.</strong> A vehicle knows its spots and a spot knows its vehicle. That makes \"where is this car?\" and \"who's in this spot?\" both instant, but the two sides must be updated together. Keeping every change inside <code>Level.park</code> and <code>ParkingLot.leave</code> is what keeps them in step.</p>\n<p><strong>Counts that can drift.</strong> <code>free_by_size</code> duplicates information you could get by scanning every spot. It's faster, but if code anywhere changes a spot without telling its level, the count goes wrong. The alternative is to recompute on demand. We keep the count and make <code>Level</code> and <code>ParkingLot</code> the only code that parks or frees vehicles.</p>","headings":[{"id":"design-choices-and-trade-offs","title":"Design choices and trade-offs","depth":3}]},{"id":"extensions-and-follow-up-questions","title":"Extensions and follow-up questions","html":"<h3 id=\"extensions-and-follow-up-questions\">Extensions and follow-up questions</h3>\n<p><strong>Payments and tickets.</strong> Add a <code>Ticket</code> class with an id, the plate, the spots, the entry time and an exit time. <code>park</code> returns a ticket instead of <code>True</code>, and <code>leave</code> takes the ticket. A <code>RateCard</code> object (a price per hour for each vehicle type) computes the fee from the two times, and a <code>Payment</code> records how it was paid. Keeping pricing in its own class means a change to the prices never touches the parking logic. For how real systems avoid charging twice when a payment is retried, see <a href=\"/learn/design-payment-system#idempotency-keys\">Design a payment system</a>.</p>\n<p><strong>An availability display.</strong> The sign at the entrance that says \"Level 2: 14 compact free\" is just <code>free_spots()</code>. To keep the sign up to date without polling, let <code>Level</code> notify listeners whenever a count changes. This is the <strong>observer pattern</strong>: the level keeps a list of callbacks and calls each one after parking or freeing a spot, without knowing whether the listener is a sign, a mobile app or a log.</p>\n<p><strong>Two cars claiming the same spot.</strong> With two entrances, two threads could run <code>find_run</code> at the same time, both see spot 7 free, and both park there. The simplest fix is a lock around every change:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> threading</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parking_lot </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ParkingLot, Vehicle</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> ThreadSafeParkingLot</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">ParkingLot</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"One lock around every change, so two cars can't claim the same spot.\"\"\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, levels):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        super</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">().</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">__init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(levels)</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._lock </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> threading.Lock()</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> park</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, vehicle: Vehicle) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        with</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._lock:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> super</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">().park(vehicle)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> leave</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, license_plate: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) -> Vehicle:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        with</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._lock:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> super</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">().leave(license_plate)</span></span></code></pre>\n<p>With a single lock, only one arrival or departure runs at a time. Our test sends 50 cars at a lot with 10 compact spots from 50 threads, and exactly 10 park. A single lock limits the lot to one change at a time, which is plenty for a garage. A busier system could lock per level, but then a bus search that spans levels must take locks in a fixed order to avoid deadlock. Once the state lives in a database, the same problem is solved with a transaction or a conditional update such as \"set vehicle = X where vehicle is null\". See <a href=\"/learn/relational-databases#isolation-transactions-dont-see-each-others-half-finished-work\">isolation in relational databases</a>.</p>\n<p><strong>EV charging spots.</strong> An electric car may want a spot with a charger. One option is a new <code>SpotSize</code> member. That's the wrong axis, though: a charger can be fitted to a compact or a large spot. A better model is a separate set of <strong>features</strong> on each spot, such as <code>{CHARGER}</code>. The vehicle states what it requires (<code>requires = {CHARGER}</code> for a car that must charge), and the search accepts a spot only if its features include them. Charging also adds new rules to discuss: a time limit, a fee per kWh, and what to do when the car is full but still parked.</p>\n<p><strong>Other follow-ups worth a sentence each.</strong> Reserved spots (a <code>reserved_for</code> field checked by the search). Monthly permits (a lookup by plate before the search). Lot-wide full signs (sum the levels' counts). Handicapped spots (another feature, like the charger).</p>","headings":[{"id":"extensions-and-follow-up-questions","title":"Extensions and follow-up questions","depth":3}]},{"id":"check-yourself","title":"Check yourself","html":"<h3 id=\"check-yourself\">Check yourself</h3>\n<details>\n<summary>Why is `Vehicle` an abstract base class instead of a regular class?</summary>\n<p>Because there's no such thing as a plain vehicle in this lot. Every vehicle is a motorcycle, a car or a bus, and each one must say which spot sizes it fits. Marking <code>fits_in</code> as abstract makes Python refuse to create a <code>Vehicle</code> directly, or a subclass that forgot to implement <code>fits_in</code>. You get a <code>TypeError</code> when the object is created instead of a wrong answer later.</p>\n</details>\n<details>\n<summary>A row is laid out compact, large, large, large, large, large, large. A car is parked in the second large spot. Can a bus park in this row?</summary>\n<p>No. The large spots are positions 1 to 6, and the car is in position 2. That leaves a run of one large spot before the car and a run of four after it. Neither reaches five, so <code>find_run</code> returns nothing. The row has five free large spots in total, which is why counting free spots isn't enough: they must be next to each other.</p>\n</details>\n<details>\n<summary>Why does a motorcycle try a motorcycle spot before a compact or large one?</summary>\n<p>To keep the bigger spots for vehicles that need them. A motorcycle can use any spot, but a car can't use a motorcycle spot and a bus can only use large ones. If motorcycles took the first free spot of any size, they could scatter across the large spots and break up the runs of five that buses need. Trying sizes from smallest to largest is a simple best-fit rule.</p>\n</details>\n<details>\n<summary>How would you add a van that needs two adjacent compact or large spots?</summary>\n<p>Write one subclass: <code>class Van(Vehicle)</code> with <code>spots_needed = 2</code> and a <code>fits_in</code> that accepts <code>COMPACT</code> and <code>LARGE</code>. Nothing else changes. <code>Level.park</code> already tries each accepted size, and <code>Row.find_run</code> already looks for a run of <code>spots_needed</code> adjacent spots. Note one limit of our search: it looks for a run of a single size, so two adjacent spots where one is compact and one is large wouldn't count. Allowing mixed runs would mean checking <code>fits_in</code> on each spot instead of comparing sizes.</p>\n</details>\n<details>\n<summary>Two entrance gates call `park` at the same moment and both pick spot 12. What went wrong, and what's the simplest fix?</summary>\n<p>Both threads read the spot as free before either one wrote to it, a race condition. The simplest fix is a lock around <code>park</code> and <code>leave</code>, so that finding a spot and occupying it happen as one step. <code>ParkingSpot.occupy</code> raising on a taken spot is a useful second line of defence, because it turns a silent double booking into a visible error.</p>\n</details>\n<details>\n<summary>Why keep a count of free spots per size when you could just scan the spots?</summary>\n<p>Two reasons. It lets a level skip a size immediately when it can't have enough spots, which the second bus in the example shows. And it gives the availability display its numbers without a scan. The cost is that the count must be updated on every park and every departure. If any code changes a spot without telling its level, the count drifts.</p>\n</details>\n<h3 id=\"further-reading\">Further reading</h3>\n<ul>\n<li><a href=\"https://docs.python.org/3/library/abc.html\" target=\"_blank\" rel=\"noopener noreferrer\">abc: Abstract Base Classes</a>: Python's documentation for <code>ABC</code> and <code>abstractmethod</code>, which this design uses for <code>Vehicle</code>.</li>\n<li><a href=\"https://docs.python.org/3/library/enum.html\" target=\"_blank\" rel=\"noopener noreferrer\">enum: Support for enumerations</a>: the documentation for <code>Enum</code> and <code>IntEnum</code>, used here for spot sizes.</li>\n<li><a href=\"https://docs.python.org/3/library/threading.html#lock-objects\" target=\"_blank\" rel=\"noopener noreferrer\">threading: Lock objects</a>: how <code>threading.Lock</code> works and how to use it with a <code>with</code> statement, as in the concurrency extension.</li>\n<li><a href=\"https://martinfowler.com/bliki/TellDontAsk.html\" target=\"_blank\" rel=\"noopener noreferrer\">Tell Don't Ask</a>: Martin Fowler's short 2013 note on asking an object to apply its own logic, which is why the level asks each vehicle whether it fits.</li>\n<li><a href=\"/learn/ood-online-chat\">Design a chat server</a>: the other object-oriented design question in the handbook, with friend requests, chats and a service that ties them together.</li>\n<li><a href=\"/learn/system-design-interview\">The system design interview, step by step</a>: the same habit of clarifying scope first, applied to whole systems.</li>\n</ul>","headings":[{"id":"check-yourself","title":"Check yourself","depth":3},{"id":"further-reading","title":"Further reading","depth":3}]}]}