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_id is the unique identifier for each seat (auto-increment)
  • free indicates 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
๐ŸŽฌ Cinema Consecutive Seats Problem๐ŸŽฌ SCREEN1Occupied2Available3Available4Available5OccupiedConnectedConnectedโœ… Result: Consecutive Available Seats[2, 3, 4]Seats 2, 3, and 4 are all available and have available neighborsSeat 1 and 5 are occupied, so they don't qualify๐Ÿ”ด Occupied ๐ŸŸข Available ๐Ÿ’› Connected Group
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for join results and final output

n
2n
โšก 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
Asked in
Facebook 15 Amazon 12 Google 8 Microsoft 6
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen