AnalystPath

Longest Gap Between Library Visits

PandasMediumMid level~10 min

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.

library_visit
patron_idvisit_day
12025-12-01
12025-12-05
12025-12-20
22025-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_visit

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