AnalystPath

Longest On-Crew Relay Streak

SQLHardSenior level~15 min

Problem

A courier network moves parcels by relay: each `relay_hops` row records one courier handing a parcel to another at time `hop_at` (format `HH:MM`). Every courier belongs to a crew. A hop is **on-crew** when the receiving courier is on the same crew as the sending courier; otherwise the parcel left the crew and the streak resets. For each crew, ordering that crew's hops by time, find the length of the longest unbroken run of consecutive on-crew hops. Return `crew_name` and `longest_streak` for every crew that has at least one on-crew hop, ordered by `crew_name`.

Tables

Example rows — the live problem includes the full dataset.

crews
courier_idcrew_name
1Falcon
2Falcon
3Falcon
relay_hops
hop_fromhop_athop_to
100:052
200:073
300:084

Expected output

Your answer should return 2 rows with the columns crew_name, longest_streak.

Starter code (SQL)

SELECT *
FROM crews;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions