AnalystPath

Longest On-Crew Relay Streak

PandasHardSenior level~10 min

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.

relay_hops
hop_fromhop_athop_to
100:052
200:073
300:084
400:105
600:157
crews
courier_idcrew_name
1Falcon
2Falcon
3Falcon
4Falcon
5Otter

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_hops

Solve 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

Related Pandas questions