AnalystPath

Longest On-Time Streak

PandasHardSenior level~10 min

Problem

You are given one DataFrame `deliveries` with columns `courier_id`, `shift_date`, and `status`. The pair `(courier_id, shift_date)` is unique, and `status` is one of `'OnTime'`, `'Late'`, or `'Missed'`.

An on-time streak is a run of consecutive shift dates (in date order) for a courier where every shift is `'OnTime'`, with no `'Late'` or `'Missed'` shift breaking the run. For each courier return `courier_id` and `best_streak`: the length of their longest on-time streak. A courier who was never on time returns 0. Row order does not matter.

Input data

Example rows — the live problem includes the full dataset.

deliveries
courier_idshift_datestatus
12024-03-01OnTime
12024-03-02OnTime
12024-03-05OnTime
12024-03-09Late
12024-03-12OnTime

Expected output

Your answer should return 3 rows with the columns courier_id, best_streak.

Starter code (Pandas (Python))

import pandas as pd

def longest_on_time_streak(deliveries) -> pd.DataFrame:
    # Your code here
    return deliveries

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