Longest Gap Between Library Visits
Problem
You are given a DataFrame `library_visit` with columns `patron_id` and `visit_day` (a date string). For each patron, the gaps to measure are the day differences between each visit and that patron's next visit, in date order. The period closes on the reference date `2026-01-01`, so the last visit's gap is measured up to `2026-01-01`.
For each patron return the longest such gap, in whole days. Order the result by `patron_id`. Output columns: `patron_id`, `longest_gap`.
Input data
Example rows — the live problem includes the full dataset.
| patron_id | visit_day |
|---|---|
| 1 | 2025-12-01 |
| 1 | 2025-12-05 |
| 1 | 2025-12-20 |
| 2 | 2025-12-28 |
Expected output
Your answer should return 2 rows with the columns patron_id, longest_gap.
Starter code (Pandas (Python))
import pandas as pd
def longest_visit_gap(library_visit) -> pd.DataFrame:
# Your code here
return library_visitSolve 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