The Consecutive Available Seats interview question is a SQL-based challenge that focuses on identifying patterns in tabular data. You are given a table representing seats in a cinema, where each row contains a seat ID and a flag indicating if the seat is free or occupied. Your task is to find all seat IDs that are free and part of a sequence of at least two consecutive free seats.
This Consecutive Available Seats coding problem is a staple at Amazon and other tech companies because it evaluates a candidate's proficiency with relational databases beyond basic filtering. It tests your ability to compare a row with its neighbors, a common requirement in data analysis and scheduling applications.
This follows the Database interview pattern. To solve this, you typically use one of two methods:
Imagine a small row of seats:
The query should identify that Seats 1 and 2 are a consecutive pair, and Seats 4 and 5 are another. Seat 3 is skipped because it's occupied. The final result would be the list [1, 2, 4, 5].
When dealing with "consecutive" logic in SQL, always consider Window Functions. They are more modern, readable, and often more performant than self-joins for sequence detection.