Java Design Program · CD24
Design a Parking Lot in Java
A parking lot is a classic object-oriented design question: spots of different sizes, vehicles that need a fitting spot, tickets, and a fee when the vehicle leaves.
Problem Statement
Design a lot with small, medium and large spots. park(plate, size, now) gives the vehicle the smallest free spot that fits and returns a ticket, or FULL.
leave(ticket, now) frees the spot and returns the fee: Rs 40 for every started hour, with a minimum of one hour.
Input/Output Examples
car parked 2.5 hoursOutputRs 120Explanation: 3 started hours × Rs 40.bike parked 45 minutesOutputRs 40Explanation: the minimum is one hour.small spot taken, a bike arrivesOutputgets L1Explanation: no medium spot is free either.truck arrives, large spot takenOutputFULLExplanation: no free spot is large enough.Visual Explanation
- SpotsfreeS1M1M2L1lot of 4
- Park 4takenbikecarcarbikesecond bike gets L1
- TruckresultFULLnothing large is free rejected
- Car leavesfee150 min → 3 hRs 120, M1 freed
- Donenext car gets M1
The fee rule is max(1, ceil(minutes / 60)) × 40, written with integers as (minutes + 59) / 60.
Approaches
Both approaches allocate the same spots and charge the same fees on 5,000 random test runs. They differ in how a free spot is found.
Approach 1: Free-Spot Sets per Size (Recommended)
Idea: keep a TreeSet of free spot numbers for each size; parking polls the lowest number from the smallest size that fits.
- Model sizes as an enum ordered SMALL, MEDIUM, LARGE.
- For each size from the vehicle's size upward,
pollFirst()a free spot. - Store a ticket with the spot and the entry time.
- On leave, return the spot to its set and compute the fee.
Time: O(log s) · Space: O(s). per park or leave; s is the number of spots.
Approach 2: Scan a List of Spots
Idea: keep spot objects in a list ordered from small to large and take the first free one that fits.
- Create the spot objects in size order.
- Scan for the first free spot whose size is large enough.
- Record the plate and entry time on the spot and map the ticket to it.
Time: O(s) · Space: O(s).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Free-Spot Sets per Size (Recommended)
Finding a spot does not depend on how many spots are taken, which matters for large lots.
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
public class ParkingLotFreeSets {
enum Size { SMALL, MEDIUM, LARGE }
record Ticket(String id, String plate, Size spotSize, int spotNo, int inAt) { }
// One sorted set of free spot numbers per size: finding a spot is a lookup, not a scan
static class ParkingLot {
private final Map<Size, TreeSet<Integer>> free = new EnumMap<>(Size.class);
private final Map<String, Ticket> active = new HashMap<>();
private int nextTicket = 1;
ParkingLot(int small, int medium, int large) {
int[] counts = {small, medium, large};
for (Size s : Size.values()) {
TreeSet<Integer> set = new TreeSet<>();
for (int i = 1; i <= counts[s.ordinal()]; i++) set.add(i);
free.put(s, set);
}
}
String park(String plate, Size vehicle, int now) {
for (Size s : Size.values()) {
if (s.compareTo(vehicle) < 0) continue; // spot too small
Integer spot = free.get(s).pollFirst(); // lowest free number of this size
if (spot != null) {
Ticket t = new Ticket("T" + nextTicket++, plate, s, spot, now);
active.put(t.id(), t);
return t.id() + "@" + s.name().charAt(0) + spot;
}
}
return "FULL";
}
int leave(String ticketLabel, int now) {
Ticket t = active.remove(ticketLabel.substring(0, ticketLabel.indexOf("@")));
free.get(t.spotSize()).add(t.spotNo());
return fee(now - t.inAt());
}
int freeCount() { return free.values().stream().mapToInt(TreeSet::size).sum(); }
}
public static void main(String[] args) {
// 1 small, 2 medium and 1 large spot; times are minutes since opening
ParkingLot lot = new ParkingLot(1, 2, 1);
String t1 = lot.park("KA01-BIKE", Size.SMALL, 0);
String t2 = lot.park("KA02-CAR", Size.MEDIUM, 10);
String t3 = lot.park("KA03-CAR", Size.MEDIUM, 20);
String t4 = lot.park("KA04-BIKE", Size.SMALL, 30); // small spot taken: uses a larger one
String t5 = lot.park("KA05-TRUCK", Size.LARGE, 40); // nothing large enough is free
System.out.println("tickets: " + t1 + ", " + t2 + ", " + t3 + ", " + t4 + ", " + t5);
System.out.println("KA02-CAR leaves after 150 min, fee Rs " + lot.leave(t2, 160));
System.out.println("KA01-BIKE leaves after 45 min, fee Rs " + lot.leave(t1, 45));
System.out.println("new car gets: " + lot.park("KA06-CAR", Size.MEDIUM, 170));
System.out.println("free spots: " + lot.freeCount());
}
// Rs 40 for every started hour, with a minimum of one hour
static int fee(int minutes) {
int hours = Math.max(1, (minutes + 59) / 60);
return hours * 40;
}
}
tickets: T1@S1, T2@M1, T3@M2, T4@L1, FULL KA02-CAR leaves after 150 min, fee Rs 120 KA01-BIKE leaves after 45 min, fee Rs 40 new car gets: T5@M1 free spots: 1
Time complexity: O(log s) · Space complexity: O(s).
Approach 2: Scan a List of Spots
Simple and fine for a few hundred spots; it slows down as the lot fills.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ParkingLotScan {
enum Size { SMALL, MEDIUM, LARGE }
static final class Spot {
final Size size;
final int no;
String plate; // null when free
int inAt;
Spot(Size size, int no) { this.size = size; this.no = no; }
String label() { return "" + size.name().charAt(0) + no; }
}
// A list of spot objects ordered small to large; parking scans for the first free spot that fits
static class ParkingLot {
private final List<Spot> spots = new ArrayList<>();
private final Map<String, Spot> byTicket = new HashMap<>();
private int nextTicket = 1;
ParkingLot(int small, int medium, int large) {
for (int i = 1; i <= small; i++) spots.add(new Spot(Size.SMALL, i));
for (int i = 1; i <= medium; i++) spots.add(new Spot(Size.MEDIUM, i));
for (int i = 1; i <= large; i++) spots.add(new Spot(Size.LARGE, i));
}
String park(String plate, Size vehicle, int now) {
for (Spot s : spots) {
if (s.plate == null && s.size.compareTo(vehicle) >= 0) {
s.plate = plate;
s.inAt = now;
String id = "T" + nextTicket++;
byTicket.put(id, s);
return id + "@" + s.label();
}
}
return "FULL";
}
int leave(String ticketLabel, int now) {
Spot s = byTicket.remove(ticketLabel.substring(0, ticketLabel.indexOf("@")));
s.plate = null;
return fee(now - s.inAt);
}
int freeCount() { return (int) spots.stream().filter(s -> s.plate == null).count(); }
}
public static void main(String[] args) {
// 1 small, 2 medium and 1 large spot; times are minutes since opening
ParkingLot lot = new ParkingLot(1, 2, 1);
String t1 = lot.park("KA01-BIKE", Size.SMALL, 0);
String t2 = lot.park("KA02-CAR", Size.MEDIUM, 10);
String t3 = lot.park("KA03-CAR", Size.MEDIUM, 20);
String t4 = lot.park("KA04-BIKE", Size.SMALL, 30); // small spot taken: uses a larger one
String t5 = lot.park("KA05-TRUCK", Size.LARGE, 40); // nothing large enough is free
System.out.println("tickets: " + t1 + ", " + t2 + ", " + t3 + ", " + t4 + ", " + t5);
System.out.println("KA02-CAR leaves after 150 min, fee Rs " + lot.leave(t2, 160));
System.out.println("KA01-BIKE leaves after 45 min, fee Rs " + lot.leave(t1, 45));
System.out.println("new car gets: " + lot.park("KA06-CAR", Size.MEDIUM, 170));
System.out.println("free spots: " + lot.freeCount());
}
// Rs 40 for every started hour, with a minimum of one hour
static int fee(int minutes) {
int hours = Math.max(1, (minutes + 59) / 60);
return hours * 40;
}
}
tickets: T1@S1, T2@M1, T3@M2, T4@L1, FULL KA02-CAR leaves after 150 min, fee Rs 120 KA01-BIKE leaves after 45 min, fee Rs 40 new car gets: T5@M1 free spots: 1
Time complexity: O(s) · Space complexity: O(s).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Free-Spot Sets per Size | O(log s) | O(s) |
| Scan a List of Spots | O(s) | O(s) |
With one sorted set per size, parking and leaving cost O(log s). The scan costs O(s) in the worst case, when most spots are taken.
Interview Tips and Recommended Approach
- Start with the entities: lot, spot, vehicle size, ticket and a fee policy.
- Make the fee a separate strategy so weekend or per-vehicle rates do not change the lot.
- Store money as integers, never as
double. - Discuss concurrency: two gates parking at once must not get the same spot, so allocation must be atomic.
Recommended Approach: Free-Spot Sets per Size
Why: finding a spot stays fast however full the lot is, and the free sets make the allocation rule easy to see.