AlgoViz

Design a parking lot

medium 90 min

Model a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.

Watch lesson4:48

Pricing that changes

Time of day, clock changes, long stays

The point: count time in UTC, look up the rate in local time, add up whole cents hour by hour. Three sentences, and they prevent nearly every pricing bug.

Segments

A stay that crosses the edge of a tariff

Day 200 cents, weekend day 300, night from 22:00 to 07:00 100. Somebody parks on Friday at 20:30 for three hours. Which rate? Cut the stay into started hours and ask for each one: what was the rate when it began?

Friday 20:30 to 23:30

HourBeginsLocal rateSum
120:30200, day200
221:30200, day400
322:30100, night500
Hour 2 runs from 21:30 to 22:30 and counts as day, because it began as day. It is a rule, not a fact. Agree on it.
Python
from datetime import datetime, timedelta, timezonefrom typing import CallableWEEKDAY, WEEKEND, NIGHT = 200, 300, 100      # cents per started hourdef rate_at(local: datetime) -> int:    if local.hour < 7 or local.hour >= 22:        return NIGHT    return WEEKEND if local.weekday() >= 5 else WEEKDAYdef price_cents(entered_utc: datetime, left_utc: datetime, to_local: Callable[[datetime], datetime]) -> int:    """Time is counted in UTC. The rate of each started hour is looked up in local time."""    total, hour = 0, entered_utc    while True:        total += rate_at(to_local(hour))        hour += timedelta(hours=1)        if hour >= left_utc:            return totalutc = timezone.utcsame = lambda t: t                            # a lot whose local time is UTCfriday = datetime(2026, 7, 3, 20, 30, tzinfo=utc)print(price_cents(friday, friday + timedelta(hours=3), same))       # 200 + 200 + 100: 21:30 is still dayprint(price_cents(friday, friday + timedelta(hours=12), same))      # until Saturday 08:30
The second stay runs until Saturday 08:30: 2 day hours at 200, 9 night hours at 100, and the hour from 07:30 on Saturday at 300. 400 + 900 + 300 = 1600.
Edit and run it in your browser

DST

The night that has 25 hours

On Sunday 25 October 2026 clocks in London go back at 02:00 summer time to 01:00 winter time. A car enters at 00:30 and leaves at 03:30. The wall says three hours. Four hours have passed.

Python
from datetime import datetime, timezonefrom zoneinfo import ZoneInfolondon = ZoneInfo("Europe/London")entered = datetime(2026, 10, 25, 0, 30, tzinfo=london)   # summer timeleft = datetime(2026, 10, 25, 3, 30, tzinfo=london)      # winter time: 02:00 went back to 01:00print(left - entered)                                     # 3:00:00, the clock on the wallprint(left.astimezone(timezone.utc) - entered.astimezone(timezone.utc))   # 4:00:00, the truth
The trap in Python: subtracting two times with the same zone object ignores the change. Convert both to UTC first. In Java, LocalDateTime has the same trap and Instant does not.
NightWall clock saysTruthIf you bill the wall
Clocks go back (October)3 h4 hYou lose an hour, and a stay from 01:40 to 01:10 is negative
Clocks go forward (March), 00:30 to 03:303 h2 hThe driver pays an hour that did not exist
Store instants in UTC. Keep the zone of the car park as configuration. Use it only to look up the rate and to print.

How do you measure a stay?

BreaksLocal times
How
exit.hour - entry.hour, or LocalDateTime.
Limit
Wrong twice a year, wrong over midnight, negative in the worst case.
Choose when
Never.
WorksUTC for length, one rate
How
Instants, subtract, round up.
Limit
Cannot do night rates.
Choose when
The base design.
StrongUTC for length, local time for the rate
How
Walk hour by hour in UTC, convert each start to local time, look up the rate.
Limit
A year-long stay is 8760 steps. Fine, or use whole days first.
Choose when
Any tariff that knows the time of day.

Long stays

Three weeks at the airport

  1. 1

    Cap per day

    Whole days times the cap, plus the rest, which is capped too. From entry or by calendar day: decide. From entry is simpler, and two days never meet at midnight.

  2. 2

    Never let it overflow

    One bill will not fill 32 bits: 2000 cents a day for 10 years is 7.3 million. A year of income of all car parks will, because 32 bits end near 2.1 billion cents. Use 64 bits and stop thinking about it.

  3. 3

    Forgotten cars

    After 30 days raise an alarm, not a bill of 60000 cents. An abandoned car is a task for a person.

  4. 4

    Clock mistakes

    Exit before entry must not give a negative fee. Treat it as an error in the clock: log it, charge the minimum.

49.5 hours with a cap of 2000

2×2000+min⁡(2000, 2×200)=44002 \times 2000 + \min(2000,\ 2 \times 200) = 4400

Money

Integers, and one place to round

StepRule
StoreWhole cents plus the currency code
Multiply by a factorBasis points: 1.5 times is 15000 per 10000
RoundOnce, at the end of the bill, with a rule the accountant knows
Split taxTax first, then net = total minus tax, so that they add up
CurrencyNot all have two decimals. Yen has none
PrintOnly here does it become 6.00