Longest Run of Open Parking Bays
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.
| bay_no | is_open |
|---|---|
| 1 | 1 |
| 2 | 0 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
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 parkingbaysSolve 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