Longest On-Crew Relay Streak
Problem
You are given two DataFrames. `crews` has columns `courier_id` and `crew_name` (which crew each courier belongs to). `relay_hops` has columns `hop_from`, `hop_at`, and `hop_to`, where `hop_at` is a string time `HH:MM` recording one courier handing a parcel to another.
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, order that crew's hops by `hop_at` and find the length of the longest unbroken run of consecutive on-crew hops.
Return columns `crew_name` and `longest_streak` for every crew that has at least one on-crew hop, ordered by `crew_name`.
Input data
Example rows — the live problem includes the full dataset.
| hop_from | hop_at | hop_to |
|---|---|---|
| 1 | 00:05 | 2 |
| 2 | 00:07 | 3 |
| 3 | 00:08 | 4 |
| 4 | 00:10 | 5 |
| 6 | 00:15 | 7 |
| courier_id | crew_name |
|---|---|
| 1 | Falcon |
| 2 | Falcon |
| 3 | Falcon |
| 4 | Falcon |
| 5 | Otter |
Expected output
Your answer should return 2 rows with the columns crew_name, longest_streak.
Starter code (Pandas (Python))
import pandas as pd
def longest_relay_streak(relay_hops, crews) -> pd.DataFrame:
# Your code here
return relay_hopsSolve this Pandas question free
Write Pandas (Python) 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