How does the fee calculation work in your parking lot design?
Fast recall
Quick revision
Core answer
Understanding of Strategy pattern and ability to isolate business logic that changes frequently from stable system structure.
Hardcoding fee calculation directly in ParkingLot or ParkingTicket classes, making it impossible to change pricing rules without modifying core code.
Explain how Strategy enables A/B testing different pricing models in production by simply swapping the calculator instance at runtime.
Interview-ready guide
Interview answer
Fee calculation is the classic place to use the Strategy pattern, because pricing rules change a lot while the parking logic stays the same. I'd define a FeeCalculator interface with a single method — calculate, taking the ticket and an exit time and returning the amount. Then each pricing rule becomes its own class: HourlyFeeCalculator multiplies the parked hours by an hourly rate, FlatRateFeeCalculator returns a fixed amount, WeekendFeeCalculator uses a cheaper rate.
The ParkingLot just holds a FeeCalculator reference and delegates to it — it never knows which concrete strategy is active. That's the power of it: when the business wants surge pricing during an event, I write a new SurgePricingCalculator and inject it, and I don't touch ParkingLot or ParkingTicket at all. That's the Open/Closed Principle — open for extension, closed for modification.
Two details that show production awareness. First, I'd use BigDecimal for the money, never double or float, because floating point introduces rounding errors that compound in financial systems. Second, I'd pass the exit time into the method rather than calling now inside it, so the calculation is deterministic and I can unit-test it with a fixed clock.
The mistake I'd call out is putting an if-else or switch on the pricing mode directly inside ParkingLot. Every new rule then means editing and retesting that method. With Strategy, a new rule is just a new class — that's the whole point.
How does the fee calculation work in your parking lot design
Direct Answer
Fee calculation uses the Strategy pattern. Define a FeeCalculator interface with calculateFee(ParkingTicket ticket) method. Concrete implementations like HourlyFeeCalculator, FlatRateFeeCalculator, and VehicleTypeFeeCalculator contain the pricing logic. ParkingLot holds a FeeCalculator reference and delegates fee computation to it.
Deep Dive & Architecture
The ticket supplies facts; the policy supplies rules
ParkingTicket records entry time, vehicle or spot type, and any pricing zone. A FeeCalculator receives the ticket and an explicit exit time. Passing the time in keeps the calculation deterministic and easy to test.
Billing rules must state their boundaries
An hourly policy should say whether a partial hour rounds up, whether there is a grace period, and which timezone applies. For example, a car entering at 10:20 and leaving at 12:05 may be billed for two hours when the rule rounds any partial hour up. A lost-ticket fee or daily cap is another policy, not an unexplained branch in ParkingLot.
Represent money deliberately
Use BigDecimal with an explicit rounding rule, or store integer minor units such as paise/cents. Strategy is useful when several pricing rules must be selected by lot, day, or vehicle type. With one fixed rule, a small calculator class is enough; adding a pattern hierarchy before variation exists only adds ceremony.
Common Mistakes & Pitfalls
- Using
doublefor money — floating point rounds wrong. AlwaysBigDecimalfor currency. - Fee logic inside
ParkingTicket— the ticket is a dumb data holder; algorithms live in the calculator (SRP). - Calling
LocalDateTime.now()inside the calculator — passexitin so the method is testable with fixed times. - A switch on pricing mode — that's the anti-pattern Strategy removes; each branch should be a class.
Check your understanding
Answer these without looking back at the article.
- Design a parking lot system. Walk me through your approach.
- How would you extend the parking lot design for multiple locations?
- What design patterns did you apply in the parking lot design and why?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.