Quick revision
The short answer
A ParkingLot holds Levels, each Level holds Spots, and each Spot has a SpotSize enum; a Vehicle knows which size it needs. On entry we issue a Ticket, a record of id, spot and entry time, and on exit a FeePolicy interface, with implementations such as HourlyFeePolicy, prices the stay using an injected Clock. Enums model fixed sets, records model immutable values, a sealed interface can model the vehicle types, interfaces sit wherever rules change, and spot allocation is made thread-safe because gates run at the same time.
What to remember
In this answer, we saw how to split a parking lot into small types with one responsibility each: the lot and levels allocate spots, the fee policy prices a stay, and payment is separate. Enums model the fixed sizes, records model immutable tickets and money, and a sealed interface models the closed set of vehicles. Fees sit behind a strategy interface with an injected Clock so they can change and be tested, and a concurrent queue of free spots per size makes allocation safe when several gates run at once.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A ParkingLot holds Levels, each Level holds Spots, and each Spot has a SpotSize enum; a Vehicle knows which size it needs. On entry we issue a Ticket, a record of id, spot and entry time, and on exit a FeePolicy interface, with implementations such as HourlyFeePolicy, prices the stay using an injected Clock. Enums model fixed sets, records model immutable values, a sealed interface can model the vehicle types, interfaces sit wherever rules change, and spot allocation is made thread-safe because gates run at the same time.
Why this question is asked
The parking lot is the most common object-oriented design exercise because everyone understands the domain, so the interview can focus on how we split responsibilities. Interviewers look for small classes with one job each, an interface exactly where requirements change, the right modern Java tool for each kind of type, and awareness that two gates can try to take the same spot at once. In this answer, we will find the main types, decide which should be enums, records and interfaces, write the core code, make fees replaceable and testable, make allocation thread-safe, and then extend the design for electric car spots.
1Finding the Main Types
We start from the nouns in the requirements and keep only those with behaviour or identity. The parking lot has levels; a level has spots; a spot has a size and is either free or taken. Vehicles come in three kinds, each needing a spot size. Entry produces a ticket, and exit produces a fee from rates that change over time.
So the main types are ParkingLot, Level, Spot, SpotSize, Vehicle, Ticket and FeePolicy. Just as important is what each one is responsible for. The lot allocates and releases spots. The fee policy turns a stay into an amount. Taking the payment is a separate step that neither of them knows about, because payment methods change for reasons that have nothing to do with parking rules.
| Type | Java construct | Why |
|---|---|---|
SpotSize, VehicleType | enum | fixed, known set of values |
| Vehicle | sealed interface + records | closed set of kinds, immutable data |
| Ticket, Money | record | immutable values compared by content |
| Spot | class | has identity and changing state (free or taken) |
Level, ParkingLot | class | own spots and the allocation logic |
FeePolicy | interface | rates change: one implementation per pricing rule |
2Enums, Records and a Sealed Interface
An enum is the right tool for a fixed set of values known at compile time. Spot sizes and vehicle types are such sets. A record is a compact class for immutable data: Java generates the constructor, accessors, equals, hashCode and toString from the components, and the fields cannot change. A ticket never changes after it is printed, so it is a record, and so is an amount of money.
A sealed interface lists exactly which classes may implement it. Since our vehicle kinds are a closed set, a sealed Vehicle with three record implementations lets the compiler check that a switch over vehicles handles every kind.
Notice that the Spot class does not hold a mutable free flag here. We will keep the free spots in the level's own structure instead, which makes the thread-safety story much simpler.
3Fees That Change: a Strategy Interface
Rates change: weekday hourly rates, a flat weekend price, a lost-ticket fee. If we write that logic inside ParkingLot, every change to prices means changing the class that also allocates spots. Instead we put pricing behind an interface. This is the Strategy pattern: one interface, several interchangeable implementations, chosen by configuration.
A FlatWeekendPolicy or a policy that combines several others can be added without touching ParkingLot. The second important detail is time. The lot gets the current time from an injected java.time.Clock rather than calling Instant.now() directly. In a test we pass Clock.fixed(...) or a mutable test clock, so we can check that 2 hours 10 minutes costs 9.00 without waiting two hours.
4Allocating Spots Safely When Gates Run Concurrently
A real lot has several entry gates, each served by its own thread or request. If two cars arrive at the same moment and both gates read that spot B-12 is free, both may assign it. That is a race condition, and the design must prevent it.
The simplest structure that avoids it is a concurrent queue of free spots per size on each level. Taking a spot is poll(), which removes and returns one element atomically, so two threads can never receive the same spot. Releasing a spot is offer(). If we needed more complex rules, a synchronized allocate method would also be correct and is easy to reason about.
4 messages in order. Play them, or step through them one at a time.
poll() is atomic, so only one gate can receive B-12.
EnumMap is a compact map for enum keys, and since the map itself never changes after construction, only the queues need to be concurrent. If the business allows a motorcycle to use a car spot when small ones are full, that rule goes into a small fallback order per size, still inside the lot.
5Follow-up questions
How would you add reserved spots for electric cars?
I would not add a boolean flag that every method must check. Charging is a capability of a spot, so the spot gets a set of features, such as EnumSet.of(Feature.EV_CHARGER), and the level keeps separate free queues for charger spots. An ElectricCar vehicle type, added to the sealed interface, asks for a medium spot with a charger first and, if the business allows it, falls back to a normal one. Pricing for charging becomes another FeePolicy, or a decorator that adds the energy cost to the parking fee, so existing policies stay unchanged.
Why not make Ticket a class with setters?
A ticket describes something that already happened: this vehicle entered this spot at this time. It should never change, and a record guarantees that and gives us equals and hashCode for free, which helps when tickets are kept in maps or compared in tests. The exit time and fee belong in a separate Receipt record created at exit.
Where does payment go?
Into its own component, for example a PaymentService that takes the Money from the fee policy. The lot should not know whether the driver pays by card or app, because payment changes for different reasons than parking rules.
6Conclusion
In this answer, we saw how to split a parking lot into small types with one responsibility each: the lot and levels allocate spots, the fee policy prices a stay, and payment is separate. Enums model the fixed sizes, records model immutable tickets and money, and a sealed interface models the closed set of vehicles. Fees sit behind a strategy interface with an injected Clock so they can change and be tested, and a concurrent queue of free spots per size makes allocation safe when several gates run at once.
Open with
I would start with the types: ParkingLot holds Levels, a Level holds Spots, each Spot has a SpotSize enum, and a Vehicle says which size it needs. For vehicles I would use a sealed interface with Motorcycle, Car and Truck records, because the set is closed and the data is immutable, so switches over vehicles are checked by the compiler.
Then, if they want more
On entry the lot issues a Ticket, which is a record of id, spot, vehicle and entry time, because a ticket never changes. Money is a record too, with BigDecimal. Pricing goes behind a FeePolicy interface, the Strategy pattern, with implementations like HourlyFeePolicy and FlatWeekendPolicy, so rate changes do not touch allocation code.
The lot takes a Clock in its constructor so I can test that two hours ten minutes costs three started hours.
Since several gates run at the same time, allocation must be thread-safe; I would keep a ConcurrentLinkedQueue of free spots per size on each level, so poll is atomic and two gates never get the same spot, or use a synchronized allocate method if the rules get more complex. Payment is a separate component.
For electric car spots, I would give spots a set of features and separate free queues rather than a flag checked everywhere.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the classes for a parking lot in Java: several levels, spots for motorcycles...": https://scrivio-interview-library.netlify.app/java/coding-problems/4c945125cc/