AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

03 Parking lot / Lesson 1 of 1

Parking lot

12 min · +10 XP · 2 exercises · 4 questions

Read the text12 min
Exercises0/2
Quiz4 q

Quick answer

How do you code a parking lot system in Java for a machine-coding round?

Model a ParkingLot that owns Levels of sized ParkingSpots, a Vehicle whose type knows the spot size it needs, and a Ticket recording the entry time. Parking asks a SpotStrategy for a free spot that fits; leaving frees the spot and asks a PricingStrategy for the fee. Both rules are interfaces, so a new allocation or tariff is a new class.

The warm-up of the machine-coding round, and the best place to show two patterns working together. The full design discussion — gates, concurrency, display boards — is in the note Design a Parking Lot. Here we code the core.

The model

  • ParkingLot owns Levels; a level owns ParkingSpots (composition).
  • A spot has a Size — S < M < L — and holds 0..1 Vehicle.
  • A VehicleType knows the smallest size it needs: bike S, car M, truck L. "Fits" is one comparison: free && size >= needs.
  • A Ticket remembers the vehicle, the spot and the entry hour.
Parking lot classes: composition below, a strategy beside

ParkingLot is what the gates call: park issues a ticket, leave returns a receipt. It holds the levels and two strategies, and decides neither the spot nor the fee itself.

0:01 / 0:14Step 1 of 4
Transcript · 4 steps

Strategy, twice

The lot never decides which spot or how much. It asks:

  • SpotStrategy.find(levels, vehicle) — here best fit: lowest level first, then the smallest free spot that fits, then the order the spots were listed.
  • PricingStrategy.fee(ticket, exitHour) — hourly (a rate per type, at least one hour) or flat.

A new tariff is a new class; the lot can even switch tariff while running, which is what the second program's PRICING command does.

Best fit: lowest level first, then the smallest spot that fits
level 1: S1 M1 L1 · level 2: M1 M2

Two levels, all spots free. A bike fits any spot, a car a medium or large one, a truck only a large one; best fit tries level 1 before level 2.

0:01 / 0:19Step 1 of 7
Transcript · 7 steps

Factory and errors

  • A VehicleFactory turns "CAR" into a Vehicle and refuses an unknown type in one place.
  • Refusals (already parked, not parked, unknown type) are exceptions printed once by main; no spot is a normal answer, not an error.

The traps

  • if-chains on type inside park: a new vehicle type edits the lot. Put the size on the type.
  • Fee logic in ParkingLot: the lot changes every time marketing changes the tariff.
  • Searching by HashMap iteration: spot order must be the order you were given — keep spots in a List.

Common questions

What is the best-fit spot allocation strategy?

Try the lowest level first and, on that level, take the smallest free spot that fits the vehicle, so large spots stay free for vehicles that need them. A bike takes a small spot before a medium one, and a car never takes a large spot while a medium one is free on the same level.

Why use the Strategy pattern for parking fees?

Tariffs change: hourly, flat at weekends, first hour free. With a PricingStrategy interface the lot calls one method and each tariff is its own class, so a new rule never edits ParkingLot, and the tariff can even be switched while the lot is running.

How should vehicle sizes be modelled?

As an enum of spot sizes ordered small to large, and a vehicle type that records the smallest size it needs. A spot fits a vehicle when it is free and its size is at least that need, which is one comparison instead of a table of if statements.

How do you compute the parking fee in hours?

Keep the entry time on the ticket and pass the exit time to the pricing strategy. An hourly tariff charges the rate for the hours parked with a minimum of one, so a vehicle that leaves in the hour it arrived still pays for one hour.

Mark as readSign in to keep your place.

Parking Lot in Java: Best-Fit Spots and a Pricing Strategy

Code a parking lot in Java: levels of sized spots, a best-fit SpotStrategy, a vehicle factory, tickets and an hourly or flat PricingStrategy swapped at runtime.

How do you code a parking lot system in Java for a machine-coding round?

Model a ParkingLot that owns Levels of sized ParkingSpots, a Vehicle whose type knows the spot size it needs, and a Ticket recording the entry time. Parking asks a SpotStrategy for a free spot that fits; leaving frees the spot and asks a PricingStrategy for the fee. Both rules are interfaces, so a new allocation or tariff is a new class.

Lesson

The warm-up of the machine-coding round, and the best place to show two patterns working together. The full design discussion — gates, concurrency, display boards — is in the note Design a Parking Lot. Here we code the core.

The model

  • ParkingLot owns Levels; a level owns ParkingSpots (composition).
  • A spot has a Size — S < M < L — and holds 0..1 Vehicle.
  • A VehicleType knows the smallest size it needs: bike S, car M, truck L. "Fits" is one comparison: free && size >= needs.
  • A Ticket remembers the vehicle, the spot and the entry hour.
ParkingLot- levels: List<Level>- strategy: SpotStrategy- pricing: PricingStrategy+ park(v, hour): Ticket+ leave(plate, hour): ReceiptLevel- number: int- spots: List<Spot>+ free(): intSpot- id: String- size: Size- vehicle: Vehicle+ fits(v): boolean1..*1..*Vehicle- plate: String- type: VehicleType0..1«interface»SpotStrategy+ find(levels, v): SpotBestFit+ find(levels, v): Spot
Parking lot classes: composition below, a strategy beside.
  1. ParkingLot is what the gates call: park issues a ticket, leave returns a receipt. It holds the levels and two strategies, and decides neither the spot nor the fee itself.
  2. The lot owns its levels and each level owns its spots — composition both times. A level keeps its spots in a List, in the order they were given.
  3. A spot holds at most one vehicle. Its Size and the vehicle type's need are both ordered S, M, L, so fits is a single comparison: free, and size at least the need.
  4. Which spot is the strategy's business. The lot calls SpotStrategy.find; BestFit is one implementation, and a nearest-to-the-lift rule would be another class.

Strategy, twice

The lot never decides which spot or how much. It asks:

  • SpotStrategy.find(levels, vehicle) — here best fit: lowest level first, then the smallest free spot that fits, then the order the spots were listed.
  • PricingStrategy.fee(ticket, exitHour) — hourly (a rate per type, at least one hour) or flat.

A new tariff is a new class; the lot can even switch tariff while running, which is what the second program's PRICING command does.

level 1KA02S1 · SKA05M1 · MKA03L1 · Llevel 2KA04M1 · MM2size MPARK KA05 CAR→ KA05 parked at level 1 spot M1
Best fit: lowest level first, then the smallest spot that fits. Example: level 1: S1 M1 L1 · level 2: M1 M2
  1. Two levels, all spots free. A bike fits any spot, a car a medium or large one, a truck only a large one; best fit tries level 1 before level 2.
  2. KA01, a car needing size M: on level 1, M1 and L1 fit a car; best fit takes the smallest, M1, keeping bigger spots for bigger vehicles.
  3. KA02, a bike needing size S: on level 1, S1 and L1 fit a bike; best fit takes the smallest, S1, keeping bigger spots for bigger vehicles.
  4. KA03, a truck needing size L: on level 1 only L1 fits a truck, so that is the spot.
  5. KA04, a car needing size M: level 1 has no free spot that fits a car, so best fit moves to level 2 and takes M1.
  6. KA01 leaves and level 1 spot M1 is free again. Nothing else moves: parked vehicles keep their spots.
  7. KA05, a car needing size M: on level 1 only M1 fits a car, so that is the spot.

Factory and errors

  • A VehicleFactory turns "CAR" into a Vehicle and refuses an unknown type in one place.
  • Refusals (already parked, not parked, unknown type) are exceptions printed once by main; no spot is a normal answer, not an error.

The traps

  • if-chains on type inside park: a new vehicle type edits the lot. Put the size on the type.
  • Fee logic in ParkingLot: the lot changes every time marketing changes the tariff.
  • Searching by HashMap iteration: spot order must be the order you were given — keep spots in a List.

Common questions

What is the best-fit spot allocation strategy?

Try the lowest level first and, on that level, take the smallest free spot that fits the vehicle, so large spots stay free for vehicles that need them. A bike takes a small spot before a medium one, and a car never takes a large spot while a medium one is free on the same level.

Why use the Strategy pattern for parking fees?

Tariffs change: hourly, flat at weekends, first hour free. With a PricingStrategy interface the lot calls one method and each tariff is its own class, so a new rule never edits ParkingLot, and the tariff can even be switched while the lot is running.

How should vehicle sizes be modelled?

As an enum of spot sizes ordered small to large, and a vehicle type that records the smallest size it needs. A spot fits a vehicle when it is free and its size is at least that need, which is one comparison instead of a table of if statements.

How do you compute the parking fee in hours?

Keep the entry time on the ticket and pass the exit time to the pricing strategy. An hourly tariff charges the rate for the hours parked with a minimum of one, so a vehicle that leaves in the hour it arrived still pays for one hour.

Exercises

Park and leave, best fit

The first line of input is the number of levels n; the next n lines list each level's spot ids, in order. Spot ids start with their size — S1 small, M2 medium, L1 large. A bike fits any spot, a car a medium or large one, a truck only a large one. Then commands, one per line:

CommandPrints
PARK <plate> <BIKE|CAR|TRUCK><plate> parked at level <L> spot <id>, or no spot for <plate>
LEAVE <plate><plate> left level <L> spot <id>
STATUSeach level as level <L>: <k> free or level <L>: full, joined by ,

Best fit: try the lowest level first; on a level, take the smallest free spot that fits; among spots of that size, the one listed first. Move to the next level only when nothing on this one fits.

Refusals print error: <reason>: unknown vehicle type <T> (checked first), <plate> is already parked, <plate> is not parked, unknown command <WORD>.

Tickets and a pricing strategy

One level this time: the first line lists its spot ids. Spot ids start with their size — S1 small, M2 medium, L1 large. A bike fits any spot, a car a medium or large one, a truck only a large one. Allocation is best fit, as before. Parking issues a ticket with the entry hour; leaving charges a fee with the pricing strategy in force at that moment, which starts as hourly.

  • Hourly: BIKE 10, CAR 20, TRUCK 40 for each hour parked (exit − entry), at least one hour.
  • Flat: the same amount whatever the vehicle and the time.
CommandPrints
PARK <plate> <TYPE> <hour><plate> parked at <id>, or no spot for <plate>
LEAVE <plate> <hour><plate> left <id>, paid <fee>
PRICING HOURLY / PRICING FLAT <amount>pricing: hourly / pricing: flat <amount>
EARNINGSearnings: <total of every fee charged>

Refusals print error: <reason>: unknown vehicle type <T>, <plate> is already parked, <plate> is not parked, unknown pricing <WORD>, unknown command <WORD>.

In this module: Parking lot

← Checkpoint — LRU cache · Checkpoint — Parking lot →