Consecutive Available Seats - Problem
You're managing seat reservations for a movie theater! Given a Cinema table with seat information, you need to find all seats that are part of consecutive groups of available seats.
The table structure is:
+-------------+------+ | Column Name | Type | +-------------+------+ | seat_id | int | | free | bool | +-------------+------+
Where:
seat_idis the unique identifier for each seat (auto-increment)freeindicates availability: 1 (true) = available, 0 (false) = occupied
Goal: Find all seat IDs that are part of consecutive available seat groups. A seat qualifies if it has at least one adjacent available seat (either left or right neighbor).
Return: All qualifying seat IDs ordered by seat_id in ascending order.
Note: The problem guarantees that more than two consecutive seats are available somewhere in the cinema.
Input & Output
example_1.sql โ Basic consecutive seats
$
Input:
Cinema table:
+--------+------+
|seat_id | free |
+--------+------+
| 1 | 0 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 0 |
+--------+------+
โบ
Output:
+--------+
|seat_id |
+--------+
| 2 |
| 3 |
| 4 |
+--------+
๐ก Note:
Seats 2, 3, and 4 are consecutive available seats. Seat 2 has seat 3 as available neighbor, seat 3 has both seats 2 and 4 as neighbors, and seat 4 has seat 3 as neighbor.
example_2.sql โ Multiple consecutive groups
$
Input:
Cinema table:
+--------+------+
|seat_id | free |
+--------+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 0 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+--------+------+
โบ
Output:
+--------+
|seat_id |
+--------+
| 1 |
| 2 |
| 4 |
| 5 |
| 6 |
+--------+
๐ก Note:
Two groups of consecutive seats: [1,2] and [4,5,6]. All seats in these groups have at least one available adjacent seat.
example_3.sql โ Single available seats excluded
$
Input:
Cinema table:
+--------+------+
|seat_id | free |
+--------+------+
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 1 |
| 5 | 1 |
| 6 | 0 |
+--------+------+
โบ
Output:
+--------+
|seat_id |
+--------+
| 4 |
| 5 |
+--------+
๐ก Note:
Seat 2 is available but isolated (no available neighbors), so it's excluded. Only seats 4 and 5 form a consecutive group.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Available Seats
Find all seats where free = 1 (green seats in the diagram)
2
Check for Neighbors
For each available seat, check if adjacent seats (ยฑ1 seat_id) are also available
3
Include Grouped Seats
Only include seats that have at least one available neighbor
4
Return Sorted Results
Return all qualifying seat_ids in ascending order
Key Takeaway
๐ฏ Key Insight: Use self-joins to efficiently find seats with available neighbors in a single database query, rather than checking each seat individually with subqueries.
Time & Space Complexity
Time Complexity
O(n log n)
Database can use indexes for efficient joins and sorting
โก Linearithmic
Space Complexity
O(n)
Space for join results and final output
โก Linearithmic Space
Constraints
- 1 โค seat_id โค 1000
- free is either 0 or 1
- More than two consecutive seats are available in each test case
- seat_id is unique and auto-incrementing
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code