Find Peak Concurrent Delivery Drivers per Station per Day

Last updated: November 29, 2026

Quick Overview

Given driver_sessions (station_id, driver_id, clock_in, clock_out) for delivery drivers at Amazon delivery stations, return for each station and day the maximum number of drivers clocked in at the same time and the first moment that peak was reached. A driver clocking out at the same instant another clocks in does not overlap.

Amazon
Data Manipulation (SQL/Python)
Data Engineer
Amazon
November 29, 2026
Data Engineer
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Given driver_sessions (station_id, driver_id, clock_in, clock_out) for delivery drivers at Amazon delivery stations, return for each station and day the maximum number of drivers clocked in at the same time and the first moment that peak was reached. A driver clocking out at the same instant another clocks in does not overlap.

Delivery stations plan parking, loading bays and van staging around peak concurrent drivers, not total drivers. This sweep-line problem in SQL is a favorite in Amazon onsites because it requires turning intervals into events, a running sum with a careful tie-break, and a second ranking step.

What the Interviewer Expects
  • Convert each interval into a +1 start event and a -1 end event with UNION ALL
  • Compute the running sum with ORDER BY ts and a tie-break that processes ends before starts
  • Partition by station and day, and state how midnight-spanning shifts are assigned
  • Find the max and the first time it occurs with a ranked subquery
  • Explain why a self-join counting overlaps is O(n squared)
Key Topics to Cover
sweep line
running sum
interval overlap
tie-breaking
window functions
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you return the full time ranges during which the station was at peak?
  • How would you handle shifts that cross midnight?
  • How would you compute the peak per 15-minute bucket for a staffing chart?
  • What if clock_out is NULL for drivers who forgot to clock out?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data
  • driver_sessions(station_id, driver_id, clock_in, clock_out), one row per shift, clock_out > clock_in, no NULL clock-outs (I would handle those with a cap like 10 hours).
  • Intervals are half-ope...
Approach

Sweep line in SQL:

  1. Emit +1 at each clock-in and -1 at each clock-out with UNION ALL.
  2. Running SUM(delta) per station and day ordered by ts, then by delta so -1 sorts before +1 at...

Submit Your Answer
Markdown supported