DSA Tracker

Low-level design

Design a parking lot

Design the classes for a multi-floor parking lot that issues a ticket on entry and charges on exit.

Good first question for: Fresher

Requirements to confirm first

  • Several floors, each with spots of fixed sizes: motorcycle, car, large
  • On entry: find a free spot that fits the vehicle, mark it taken, issue a ticket with the entry time
  • On exit: compute the fee from the time parked and the spot size, take payment, free the spot
  • Show how many spots of each size are free
  • Ask before assuming: one gate or many, reserved or electric spots, hourly or flat pricing

Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.

Show the answer key (write yours first)

Classes and what each one owns

ParkingLot
Entry point: owns the floors, hands out tickets and closes them at exit
Floor
Owns its spots and answers 'first free spot of this size'
ParkingSpot
Knows its size and whether a vehicle is in it
Vehicle
Carries the number plate and the spot size it needs
Ticket
Links one vehicle to one spot, with entry and exit time
PricingPolicy
Turns a ticket into an amount: hourly, flat or per size
Payment
Records the amount, the method and whether it succeeded

How they connect

  • ParkingLot has many Floors and a Floor has many ParkingSpots: composition, a spot does not exist without its floor
  • A Ticket refers to a Vehicle and a ParkingSpot and owns neither
  • ParkingLot depends on the PricingPolicy and SpotFinder interfaces, never on one concrete rule
  • Vehicle size is an enum, not a subclass per vehicle, unless behaviour really differs

Design patterns that fit, and why

Strategy
Pricing and spot selection both change by business rule (hourly or flat, nearest or lowest floor), so each sits behind an interface.
Factory
Only worth it if creating a ticket or a vehicle has real branching. Say that instead of adding it by reflex.
Singleton
Often suggested for ParkingLot. One instance passed to whoever needs it is easier to test than a hidden global.

Code sketch in Java

The core types and one real method. It is a sketch to talk through, not a full program.

enum SpotSize { MOTORCYCLE, CAR, LARGE }

interface PricingPolicy { long feeInPaise(Ticket ticket); }
interface SpotFinder { Optional<ParkingSpot> find(List<Floor> floors, SpotSize size); }

class ParkingLot {
  private final List<Floor> floors;
  private final SpotFinder finder;
  private final PricingPolicy pricing;

  Ticket enter(Vehicle vehicle) { /* find a spot, occupy it, issue the ticket */ }
  long exit(Ticket ticket)      { /* stamp the exit time, price it, free the spot */ }
}

Edge cases to raise yourself

  • Two cars at two gates are given the same last spot: finding and occupying a spot must be one atomic step
  • The lot is full: return a clear 'no spot' result instead of throwing from deep inside
  • A lost ticket, or exit called twice for the same ticket
  • A car sent to a large spot when car spots run out: decide the rule and say it

Where answers usually go wrong

  • Starting with a class per vehicle type (Car, Bike, Truck) that differ only in a size field. An enum is enough; subclasses are for different behaviour.
  • Putting the fee formula inside Ticket or ParkingLot. The first follow-up is weekend pricing, and it should be a new class, not an edit.
  • Ignoring that two gates run at the same time. Say where the lock or the atomic update sits before the interviewer asks.
  • Storing money as a floating-point number. Use an integer count of the smallest unit.

Follow-up questions

  • Add electric charging spots with a different rate
  • How does the board showing free spots stay correct with many gates?
  • Support monthly passes that skip payment at exit