AnalystPath

Longest Run of Open Parking Bays

SQLMediumMid level~15 min

Problem

The table `ParkingBays` lists a parking row, one bay per row, with a `bay_no` (the position along the row) and an `is_open` flag (1 when the bay is open, 0 when it is taken). Bays are not necessarily numbered without gaps. 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. For every winning stretch report `start_bay`, `end_bay`, and `run_length`, ordered by `start_bay`.

Tables

Example rows — the live problem includes the full dataset.

ParkingBays
bay_nois_open
11
20
31

Expected output

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

Starter code (SQL)

SELECT *
FROM ParkingBays;

Solve this SQL question free

Write SQL 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 SQL questions