← Questions

Parking lot occupancy peaks from entry and exit events

0
AmazonCodingLast asked Dec 2025

A coding interview question asked in Amazon's technical phone screen, last asked Dec 2025. It tests sweep line, sorting and intervals.

A parking lot logs every car that drives in or out as [car_id, time, event], where event is "entry" or "exit" and time is an integer number of hours. The log is not sorted. A car parks over the half-open interval [entry, exit), and a car cannot enter again before it has left. When several events share a timestamp, all exits at that time happen before any entries.

Part 1: the peak

Unless you agree on a different interface with the interviewer, assume:

  • max_occupancy(logs) -> int — the largest number of cars in the lot at the same time. Every entry has a matching later exit.

Part 2: when the peak happens

  • peak_intervals(logs) -> list[list[int]] — every interval [start, end) during which the count equals the Part 1 maximum, sorted by start. Each interval is maximal: if the count stays at the maximum across a timestamp, that is one interval, not two.

Part 3: missing exits

The exit scanner is faulty: some exits were never recorded, though every entry was. No car may stay longer than 2 hours, so a car with no exit left at some unknown time in (entry, entry + 2]. Return the interval or intervals during which the lot could have held the largest possible number of cars, choosing the unknown exit times to make that peak as large as possible.

  • peak_intervals_missing(logs, max_stay=2) -> list[list[int]]

Example

logs = [[3, 9, "entry"], [1, 1, "entry"], [2, 2, "entry"], [1, 4, "exit"],
        [2, 6, "exit"], [3, 12, "exit"], [4, 3, "entry"], [4, 5, "exit"]]
max_occupancy(logs)    ->  3          # cars 1, 2 and 4 are all parked during [3, 4)
peak_intervals(logs)   ->  [[3, 4]]

logs = [[1, 1, "entry"], [1, 3, "exit"], [2, 3, "entry"], [2, 5, "exit"]]
max_occupancy(logs)    ->  1          # car 1 leaves at 3 before car 2 arrives at 3
peak_intervals(logs)   ->  [[1, 5]]   # the count is 1 throughout, so one interval

logs = [[1, 0, "entry"], [2, 1, "entry"], [2, 4, "exit"], [3, 3, "entry"]]
peak_intervals_missing(logs)  ->  [[1, 2], [3, 4]]
# cars 1 and 3 have no exit; staying the full 2 hours gives [0, 2) and [3, 5),
# so two cars are parked during [1, 2) and again during [3, 4)

Constraints

  • Up to 10^5 events; times are non-negative integers.
  • In Parts 1 and 2 every entry has a matching later exit for the same car.

Clarifying questions

  • Are timestamps integers, and is the stay limit in the same unit?
  • Should touching intervals at the maximum be merged?
  • In Part 3, how do you tell which entry an exit belongs to when a car visits several times? (Pair each exit with that car's latest earlier entry; an entry followed by another entry of the same car lost its exit.)

What a strong answer covers

  • A sweep: turn each stay into +1 at entry and -1 at exit, sort by time with exits before entries at equal times, and track the running count: O(n log n).
  • For Part 2, the stretches between consecutive event times where the count equals the maximum, merged when they touch.
  • For Part 3, the key observation: a longer stay can only raise the count, so every car without an exit should be assumed to stay the full 2 hours; then Part 2 applies unchanged.
  • Reconstructing stays per car by sorting each car's events, and detecting a missing exit.

Follow-ups

  • Answer "how many cars were parked at time t?" for many queries.
  • The log is a live stream. Keep the current count and the peak so far.
  • Exits can also be recorded without their entry. What is the lowest peak consistent with the log?

Where it's asked

  • Amazon · Technical phone screen · Last asked Dec 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

sweep line · sorting · intervals

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding