Scrivio Interview library
DesignFoundations7 min read · updated 12 November 2026

Design the classes for a parking lot in Java: several levels, spots for motorcycles, cars and trucks, tickets on entry and fees on exit with rates that change. Show the main types and explain where you would use interfaces, enums and records.

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.

Main types and the Java construct for each
TypeJava constructWhy
SpotSize, VehicleTypeenumfixed, known set of values
Vehiclesealed interface + recordsclosed set of kinds, immutable data
Ticket, Moneyrecordimmutable values compared by content
Spotclasshas identity and changing state (free or taken)
Level, ParkingLotclassown spots and the allocation logic
FeePolicyinterfacerates 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.

Java
enum SpotSize { SMALL, MEDIUM, LARGE }
sealed interface Vehicle permits Motorcycle, Car, Truck {
String plate();
SpotSize requiredSize();
}
record Motorcycle(String plate) implements Vehicle { public SpotSize requiredSize() { return SpotSize.SMALL; } }
record Car(String plate) implements Vehicle { public SpotSize requiredSize() { return SpotSize.MEDIUM; } }
record Truck(String plate) implements Vehicle { public SpotSize requiredSize() { return SpotSize.LARGE; } }
record Money(java.math.BigDecimal amount, java.util.Currency currency) {}
record Ticket(java.util.UUID id, Spot spot, Vehicle vehicle, java.time.Instant entryTime) {}
final class Spot {
private final String id;
private final SpotSize size;
Spot(String id, SpotSize size) { this.id = id; this.size = size; }
String id() { return id; }
SpotSize size() { return size; }
}

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.

Java
interface FeePolicy {
Money fee(Ticket ticket, java.time.Instant exitTime);
}
final class HourlyFeePolicy implements FeePolicy {
private final java.math.BigDecimal perHour;
HourlyFeePolicy(java.math.BigDecimal perHour) { this.perHour = perHour; }
public Money fee(Ticket t, java.time.Instant exit) {
long minutes = java.time.Duration.between(t.entryTime(), exit).toMinutes();
long hours = Math.max(1, (minutes + 59) / 60); // started hours, minimum 1
return new Money(perHour.multiply(java.math.BigDecimal.valueOf(hours)),
java.util.Currency.getInstance("USD"));
}
}
// 2 h 10 min at 3.00 per hour -> 3 started hours -> 9.00

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.

Java
final class Level {
private final java.util.Map<SpotSize, java.util.Queue<Spot>> free = new java.util.EnumMap<>(SpotSize.class);
Level(java.util.List<Spot> spots) {
for (SpotSize s : SpotSize.values()) free.put(s, new java.util.concurrent.ConcurrentLinkedQueue<>());
spots.forEach(sp -> free.get(sp.size()).offer(sp));
}
java.util.Optional<Spot> take(SpotSize size) { return java.util.Optional.ofNullable(free.get(size).poll()); }
void release(Spot spot) { free.get(spot.size()).offer(spot); }
}
final class ParkingLot {
private final java.util.List<Level> levels;
private final FeePolicy feePolicy;
private final java.time.Clock clock;
ParkingLot(java.util.List<Level> levels, FeePolicy feePolicy, java.time.Clock clock) {
this.levels = levels; this.feePolicy = feePolicy; this.clock = clock;
}
java.util.Optional<Ticket> enter(Vehicle v) {
for (Level level : levels) {
var spot = level.take(v.requiredSize());
if (spot.isPresent()) {
return java.util.Optional.of(new Ticket(java.util.UUID.randomUUID(), spot.get(), v, clock.instant()));
}
}
return java.util.Optional.empty(); // lot full for this size
}
Money exit(Ticket t, Level level) {
level.release(t.spot());
return feePolicy.fee(t, clock.instant());
}
}
Step throughTwo gates, one free car spot
Gate 1Level 2 free queueGate 2
poll() for MEDIUM
poll() for MEDIUM, at the same moment
spot B-12
null: queue now empty, try the next level

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.

What to say in the interviewAbout 80 seconds aloud

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/