Consecutive Available Seats II - Problem

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 identifier
  • free (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_id in ascending order

Output columns:

  • first_seat_id: Starting seat of the consecutive sequence
  • last_seat_id: Ending seat of the consecutive sequence
  • consecutive_seats_len: Length of the consecutive sequence

Table Schema

Cinema
Column Name Type Description
seat_id PK int Auto-increment seat identifier
free bool Seat availability (1 = free, 0 = occupied)
Primary Key: seat_id
Note: Each row represents one cinema seat and its availability status

Input & Output

Example 1 — Basic Consecutive Sequence
Input Table:
seat_id free
1 1
2 0
3 1
4 1
5 1
Output:
first_seat_id last_seat_id consecutive_seats_len
3 5 3
💡 Note:

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.

Example 2 — Multiple Equal Length Sequences
Input Table:
seat_id free
1 1
2 1
3 0
4 1
5 1
6 0
Output:
first_seat_id last_seat_id consecutive_seats_len
1 2 2
4 5 2
💡 Note:

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.

Example 3 — All Seats Occupied
Input Table:
seat_id free
1 0
2 0
3 0
Output:
first_seat_id last_seat_id consecutive_seats_len
💡 Note:

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
  • free is either 0 or 1
  • There will always be at most one longest consecutive sequence

Visualization

Tap to expand
Consecutive Available Seats II INPUT Cinema Table seat_id free 1 1 2 1 3 0 4 1 5 1 6 1 7 0 Visual Representation: 1 2 3 4 5 6 7 = Available (1) = Occupied (0) ALGORITHM STEPS 1 Filter Free Seats WHERE free = 1 2 Create Groups seat_id - ROW_NUMBER() seat_id | row_num | group 1 | 1 | 0 2 | 2 | 0 4 | 3 | 1 3 Aggregate Groups MIN, MAX, COUNT per group Group 0: start=1, end=2, len=2 Group 1: start=4, end=6, len=3 4 Find Maximum WHERE len = MAX(len) FINAL RESULT Longest Consecutive Sequence: 4 5 6 Length = 3 Output Table: start end length 4 6 3 OK - Found! Seats 4-6 (3 consecutive) Note: If multiple sequences have same max length, return ALL Key Insight: The trick is using (seat_id - ROW_NUMBER()) to create group identifiers. Consecutive seats will have the same difference value, allowing us to group and count them efficiently. Time Complexity: O(n log n) for sorting | Space Complexity: O(n) for window functions TutorialsPoint - Consecutive Available Seats II | Optimal Solution
Asked in
Netflix 15 Uber 12 Airbnb 8
28.5K Views
Medium Frequency
~20 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