You have a table Cinema with information about cinema seats and their availability status.
Table: Cinema
seat_id(int): Auto-increment column representing the seat identifierfree(bool): Indicates whether the seat is available (1 = free, 0 = occupied)
Task: Find the length of the longest consecutive sequence of available seats in the cinema.
Important notes:
- There will always be at most one longest consecutive sequence
- If there are multiple consecutive sequences with the same maximum length, include all of them
- Return results ordered by
first_seat_idin ascending order
Output columns:
first_seat_id: Starting seat of the consecutive sequencelast_seat_id: Ending seat of the consecutive sequenceconsecutive_seats_len: Length of the consecutive sequence
Table Schema
| Column Name | Type | Description |
|---|---|---|
seat_id
PK
|
int | Auto-increment seat identifier |
free
|
bool | Seat availability (1 = free, 0 = occupied) |
Input & Output
| seat_id | free |
|---|---|
| 1 | 1 |
| 2 | 0 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| first_seat_id | last_seat_id | consecutive_seats_len |
|---|---|---|
| 3 | 5 | 3 |
The longest consecutive sequence of available seats starts from seat 3 and ends at seat 5 with a length of 3. Seat 1 is available but isolated, while seat 2 is occupied, breaking the sequence.
| seat_id | free |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 0 |
| 4 | 1 |
| 5 | 1 |
| 6 | 0 |
| first_seat_id | last_seat_id | consecutive_seats_len |
|---|---|---|
| 1 | 2 | 2 |
| 4 | 5 | 2 |
Two consecutive sequences have the same maximum length of 2: seats 1-2 and seats 4-5. Both sequences are included in the output, ordered by first_seat_id.
| seat_id | free |
|---|---|
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| first_seat_id | last_seat_id | consecutive_seats_len |
|---|
No seats are available (all free = 0), so there are no consecutive sequences to return. The result is an empty table.
Constraints
-
1 ≤ seat_id ≤ 50 -
freeis either0or1 - There will always be at most one longest consecutive sequence