AnalystPath

Longest Run of Open Parking Bays

PandasMediumMid level~10 min

Problem

You are given a DataFrame `parkingbays` (CSV `parkingbays.csv`) with columns bay_no (the position along a parking row) and is_open (1 when the bay is open, 0 when it is taken). Bay numbers are not necessarily contiguous. Find the longest stretch of consecutive open bays — a run is broken whenever a bay is taken or a bay number is missing. If several stretches tie for the longest length, return each of them. Output columns: `start_bay`, `end_bay`, and `run_length`, ordered by `start_bay`.

Input data

Example rows — the live problem includes the full dataset.

parkingbays
bay_nois_open
11
20
31
41
51

Expected output

Your answer should return 1 row with the columns start_bay, end_bay, run_length.

Starter code (Pandas (Python))

import pandas as pd

def longest_run_of_open_parking_bays(parkingbays) -> pd.DataFrame:
    # Your code here
    return parkingbays

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