Consecutive Available Seats - Problem

You are given a table Cinema that contains information about cinema seats and their availability status.

Table: Cinema

Column NameType
seat_idint
freebool

seat_id is an auto-increment column for this table. Each row indicates whether the seat is available or occupied.

  • 1 means the seat is free (available)
  • 0 means the seat is occupied

Task: Find all consecutive available seats in the cinema. Return the result ordered by seat_id in ascending order.

Note: The test cases guarantee that more than two seats are consecutively available.

Table Schema

Cinema
Column Name Type Description
seat_id PK int Auto-increment primary key representing seat number
free bool 1 if seat is available, 0 if occupied
Primary Key: seat_id
Note: Seats are numbered sequentially, and we need to find groups of consecutive available seats

Input & Output

Example 1 — Mixed Available and Occupied Seats
Input Table:
seat_id free
1 1
2 1
3 0
4 1
5 1
Output:
seat_id
1
2
4
5
💡 Note:

Seats 1 and 2 are both free and consecutive. Seat 3 is occupied, so it breaks the sequence. Seats 4 and 5 are both free and consecutive. Therefore, all seats except seat 3 are part of consecutive available groups.

Example 2 — Long Consecutive Sequence
Input 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 form one continuous block of available seats. Seats 1 and 5 are occupied, so they don't appear in the result. All three consecutive seats (2, 3, 4) are returned.

Example 3 — Multiple Groups
Input Table:
seat_id free
1 1
2 1
3 0
4 0
6 1
7 1
8 1
Output:
seat_id
1
2
6
7
8
💡 Note:

Two separate groups of consecutive seats: seats 1-2 form one group, and seats 6-7-8 form another group. Seats 3 and 4 are occupied. Note that seat 5 is missing from the table, creating a gap between the groups.

Constraints

  • 1 ≤ seat_id ≤ 50
  • free is either 0 or 1
  • More than two seats are consecutively available

Visualization

Tap to expand
Consecutive Available Seats INPUT: Cinema Table seat_id free 1 1 (free) 2 0 (taken) 3 1 (free) 4 1 (free) 5 1 (free) Cinema Seats Visual: 1 2 3 4 5 Free Taken ALGORITHM STEPS 1 Self-Join Table Join Cinema with itself c1.seat_id, c2.seat_id 2 Check Adjacent ABS(c1.seat_id - c2.seat_id) = 1 Find neighboring seats 3 Filter Free Seats WHERE c1.free=1 AND c2.free=1 Both must be available 4 Get Distinct Results SELECT DISTINCT seat_id ORDER BY seat_id SELECT DISTINCT c1.seat_id FROM Cinema c1, Cinema c2 WHERE ABS(c1.seat_id - c2.seat_id) = 1 AND c1.free=1 AND c2.free=1 FINAL RESULT seat_id 3 4 5 Why these seats? 3 adjacent to 4 (both free) 4 adjacent to 3 and 5 5 adjacent to 4 (both free) Seat 1 excluded: Adjacent seat 2 is NOT free Key Insight: Use SELF-JOIN to compare each seat with all other seats. The condition ABS(seat_id difference) = 1 finds adjacent seats. Both seats must be free (free=1). DISTINCT removes duplicates since each qualifying seat may match multiple neighbors. Time: O(n^2) for join, Space: O(n) for result. TutorialsPoint - Consecutive Available Seats | Optimal Solution (Self-Join)
Asked in
Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~12 min Avg. Time
890 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