CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 5 of 6 · 3 min read

Edge cases and changes

What can go wrong, and how the design answers a change.

Step 5: edge cases, tests and change requests

A list of edge cases, each with the answer the code gives:

Edge case What happens
Lot full for this vehicle type LotFullException, nothing changes
A small spot is free but the vehicle is a car Not chosen, since a car does not fit small
Same plate enters twice Rejected with a clear message
Exit with an unknown or already used ticket NoSuchElementException
Stay of zero minutes Charged one hour, by Math.max(1, ...)
Two gates at the same instant Serialised by the lock, one gets the spot
Null vehicle or plate Rejected at construction

Tests to write first (module 5 of Level 5): a car goes to a medium spot before a large one; a truck is refused when only medium spots remain; the fee for 61 minutes is two hours; a second exit with the same ticket fails; and eight threads parking the same plate give exactly one ticket.

Change requests and the class that changes. Practise saying these aloud:

  • "Add a motorcycle type that fits small spots only." Add a value to VehicleType and one case to fits. No other class changes.
  • "Weekends cost double." Write a new FeeStrategy, or wrap HourlyFee in a decorator that doubles the result (Level 4, module 5). The lot is untouched.
  • "First 30 minutes free." A new FeeStrategy.
  • "Show free spots per floor on a board." The lot publishes an event when a spot changes, and a Display subscribes (observer, Level 4, module 2).
  • "Reserve a spot in advance." A new concept, Reservation, and a change to the choice rule. This one does touch the lot, and it is fair to say so.
  • "Many lots in a city." A City that holds lots. ParkingLot stays as it is.

One weakness is worth naming: park scans every spot, which is fine for hundreds of spots. For many thousands, keep a free list per spot type, and take from the right list. Say this as a possible next step, and not as something the round demands.

Common mistake

Starting with Car, Bike and Truck subclasses and a deep tree. The vehicles differ in data, not in behaviour, so an enum keeps the design smaller.

Interview angle

Answer in your head first, then open it.

Walk me through your classes.

Vehicle, Spot, Ticket, ParkingLot, FeeStrategy, Clock, each with one sentence on its job.

How do you pick a spot?

The smallest free spot type that fits, scanning the list. State the cost and the faster alternative.

What if two cars arrive at once?

Park and exit are synchronized, and each checks before it changes state.

How does the fee change without editing the lot?

The fee is a strategy that is injected.

Check yourself: 1. Which class changes when a motorcycle type is added? 2. Which pattern lets weekends cost double without editing HourlyFee? 3. Which change request does touch the lot, and why?

Words you will meet

Word Meaning
Out of scope Named and deliberately left out of the design
Ticket A record of which vehicle took which spot and when
Fit rule The rule for which vehicle may use which spot type
Started hour Any part of an hour, counted as a full hour
Open ticket A ticket whose vehicle has not yet left