Parking Lot Design: Smallest Fitting Spot for Three Sizes, Then EV Charging Fallback
Company: Amazon
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Design and implement a parking lot that supports three vehicle sizes and three spot sizes. Implement two operations, `park` and `leave`. When a vehicle parks, it must be given the smallest free spot that fits it. The interview was coded in C++; any object-oriented language is fine.
Assume the sizes are small, medium and large (for example a motorcycle, a car and a truck) and that a vehicle fits a spot of its own size or larger; confirm both with the interviewer.
### Clarifying Questions
- Which vehicle types and spot sizes exist, and may a vehicle use any larger spot or only certain ones?
- What does `park` return (a spot id or a ticket), and what happens when no spot fits?
- How is a vehicle identified on `leave`: by licence plate, by ticket or by spot?
- When several free spots have the smallest fitting size, which one should be chosen?
- Can several entrances call `park` at the same time?
### Part 1 — Park and leave with smallest-fit allocation
Implement the classes and the two operations so that each vehicle gets the smallest free spot that fits it.
```hint Index the free spots
Think about which structure lets `park` find a free spot of a given size without scanning the whole lot, and what `leave` must update so the spot can be found again.
```
#### What This Part Should Cover
- The classes and the responsibility of each
- The smallest-fit search and its cost
- Behavior for a full lot, a vehicle that is already parked, and an unknown vehicle on `leave`
### Part 2 — Adding EV charging spaces
Follow-up: some spaces now have chargers. Electric vehicles should prefer a charging space and other vehicles should prefer a regular space, but each may fall back to the other kind of space when its preferred kind is full.
#### Clarifying Questions for this Part
- Do charging spaces come in all three sizes?
- Should an electric car take a larger charging space before a regular space of its own size, or the other way round?
```hint Make the preference explicit
Write the search for one vehicle as an ordered list of candidate (kind, size) groups, and decide where the size rule and the charging rule rank against each other.
```
#### What This Part Should Cover
- How the precedence between spot size and charging preference is decided and stated
- Fallback in both directions without duplicating the allocation code
- How the design absorbs this change, and the next one, with little rewriting
### What a Strong Answer Covers
- Sizes, fit rules, return values and the tie rule clarified before coding
- A clean object model with the allocation logic in one place
- An efficient free-spot index that `park` and `leave` keep correct
- An explicit, configurable preference order for charging and size
- Thread-safety and the edge cases: a full lot, a double park, an unknown vehicle leaving
- Tests that cover smallest fit and fallback in both directions
### Follow-up Questions
- Two entrances call `park` at the same moment. How do you guarantee they never receive the same spot?
- How would the design change for several floors, where distance to the entrance matters as much as size?
- Non-electric cars have filled the charging spaces and electric cars keep arriving. What policy would you add, and where in the code would it live?
- How would you add billing by parking duration and spot type without touching the allocation code?
Overview: A low-level design exercise: build a parking lot with three vehicle sizes and three spot sizes whose park and leave operations assign the smallest free spot that fits, then add EV charging spaces that electric and regular vehicles prefer differently, with fallback both ways. It tests object modeling, allocation policy and extensibility.
Read the full Amazon Software Engineer interview experience this question came from