Longest On-Time Streak
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.
| courier_id | shift_date | status |
|---|---|---|
| 1 | 2024-03-01 | OnTime |
| 1 | 2024-03-02 | OnTime |
| 1 | 2024-03-05 | OnTime |
| 1 | 2024-03-09 | Late |
| 1 | 2024-03-12 | OnTime |
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 deliveriesSolve 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