Grokking the Object Oriented Design Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Kushak Zohaad Jafry
Confusing and weird pattern

Kushak Zohaad Jafry

Jan 26, 2026

Issues I found with this design:

1. Unnecessary Inheritance for ParkingSpot

The code creates separate classes (HandicappedSpot, CompactSpot, LargeSpot, etc.) that literally do nothing except pass an enum to the parent constructor. This violates "composition over inheritance."

java

// Current approach - 5 classes doing the same thing public class CompactSpot extends ParkingSpot { public CompactSpot() { super(ParkingSpotType.COMPACT); } } // Better approach - single class with composition public class ParkingSpot { private String number; private ParkingSpotType type; private boolean free; public ParkingSpot(String number, ParkingSpotType type) { this.number = number; this.type = type; this.free = true; } }

2. Poor Data Structure in ParkingFloor

Maintaining 5 separate HashMaps is a code smell:

java

private HashMap<String, HandicappedSpot> handicappedSpots; private HashMap<String, CompactSpot> compactSpots; private HashMap<String, LargeSpot> largeSpots; private HashMap<String, MotorbikeSpot> motorbikeSpots; private HashMap<String, ElectricSpot> electricSpots;

This makes the code harder to maintain. When you need to add a new spot type, you have to modify multiple places. Better approach:

java

// Single map for all spots private Map<String, ParkingSpot> allSpots; // Or grouped by type if needed private Map<ParkingSpotType, List<ParkingSpot>> spotsByType;

3. Confusing Spot Assignment Logic

The ParkingLot tracks counts but doesn't actually assign specific spots:

java

private int compactSpotCount; private int largeSpotCount;

The getNewParkingTicket() method increments counts but doesn't tell you which actual spot the vehicle is parked in. How do you find the vehicle during exit? The Ticket should contain a reference to the actual ParkingSpot, not just aggregate counts.

4. Overcomplicated Display Board

The ParkingDisplayBoard tracks ONE free spot per type and has to constantly search and update it:

java

private void updateDisplayBoardForCompact(ParkingSpot spot) { if (this.displayBoard.getCompactFreeSpot().getNumber() == spot.getNumber()) { // find another free compact parking and assign to displayBoard for (String key : compactSpots.keySet()) { if (compactSpots.get(key).isFree()) { this.displayBoard.setCompactFreeSpot(compactSpots.get(key)); } } } }

This is inefficient. Better to just show available count or use an indexed structure.

5. Missing Core Functionality

  • No actual spot-finding algorithm when vehicle enters
  • No way to locate a vehicle's spot given a ticket during exit
  • No payment/fee calculation
  • Thread safety only on ticket generation, not on spot assignment

Suggested Approach:

java

public class ParkingLot { private List<ParkingFloor> floors; private Map<String, Ticket> activeTickets; public synchronized Ticket parkVehicle(Vehicle vehicle) { // Find available spot across floors for (ParkingFloor floor : floors) { ParkingSpot spot = floor.findAvailableSpot(vehicle); if (spot != null) { spot.park(vehicle); Ticket ticket = new Ticket(vehicle, spot); // spot reference! activeTickets.put(ticket.getId(), ticket); return ticket; } } throw new ParkingFullException(); } public synchronized double unparkVehicle(String ticketId) { Ticket ticket = activeTickets.get(ticketId); ticket.getSpot().vacate(); // can access actual spot double fee = calculateFee(ticket); activeTickets.remove(ticketId); return fee; } }

This design is simpler, more maintainable, and actually tracks where vehicles are parked.

Would love to hear thoughts on this analysis!

0

0

Comments

On this page

System Requirements

Use case diagram

Class diagram

Activity diagrams

Code