Watch 10 video solutions for Human Traffic of Stadium, a hard level problem involving Database. This walkthrough by Ankit Bansal has 42,283 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Stadium
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | visit_date | date | | people | int | +---------------+---------+ visit_date is the column with unique values for this table. Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit. As the id increases, the date increases as well.
Write a solution to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each.
Return the result table ordered by visit_date in ascending order.
The result format is in the following example.
Example 1:
Input: Stadium table: +------+------------+-----------+ | id | visit_date | people | +------+------------+-----------+ | 1 | 2017-01-01 | 10 | | 2 | 2017-01-02 | 109 | | 3 | 2017-01-03 | 150 | | 4 | 2017-01-04 | 99 | | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-09 | 188 | +------+------------+-----------+ Output: +------+------------+-----------+ | id | visit_date | people | +------+------------+-----------+ | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-09 | 188 | +------+------------+-----------+ Explanation: The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7. The rows with ids 2 and 3 are not included because we need at least three consecutive ids.
Problem Overview: The Human Traffic of Stadium problem asks you to return stadium records that belong to a streak of at least three consecutive days where the number of visitors is 100 or more. The result should include every row that participates in such a consecutive sequence.
Approach 1: Iterative Comparison (O(n) time, O(1) space)
Start by filtering records where people >= 100. Sort by id or visit_date to maintain chronological order. Iterate through the filtered list and check if three adjacent rows have consecutive IDs (e.g., id, id+1, id+2). If the condition holds, include those rows in the result set. This approach works well in scripting languages like Python where you can scan arrays directly. The key insight is that consecutive traffic days form contiguous segments after filtering.
Approach 2: Sliding Window Method (O(n) time, O(1) space)
Treat the filtered dataset as a sequence and maintain a window of three records. Move the window one step at a time and check whether the IDs are consecutive and each row has people >= 100. When the window satisfies the constraint, mark all elements inside the window as valid. This technique avoids repeated comparisons and is a common pattern in problems involving contiguous segments, similar to techniques discussed in sliding window problems.
Approach 3: SQL Window Functions (O(n) time, O(n) space)
SQL provides a clean solution using LAG() and LEAD() window functions. After filtering rows where people >= 100, compare each row with its neighbors to check if three consecutive IDs exist. Another common pattern groups rows using id - ROW_NUMBER() to identify consecutive sequences. Once groups are formed, keep only groups with size ≥ 3. Window functions make this approach concise and efficient when working with relational datasets, which is why it frequently appears in SQL and database interview questions.
Approach 4: SQL-like Filter and Sort Strategy (O(n log n) time, O(n) space)
In JavaScript or other general-purpose languages, you can mimic SQL behavior by filtering rows with people >= 100, sorting them by ID, and grouping consecutive IDs into segments. Track segment boundaries by checking whether current.id == previous.id + 1. Once a segment length reaches three or more, add all rows from that segment to the result. This approach mirrors how relational engines process ordered datasets.
Recommended for interviews: The SQL window function solution is typically expected in database-focused interviews because it demonstrates familiarity with LAG, LEAD, and sequence grouping techniques. The sliding window or iterative scan shows the same logic in algorithmic form and proves you understand how to detect consecutive segments efficiently. Demonstrating both perspectives signals strong problem-solving ability across SQL and general programming environments.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Comparison | O(n) | O(1) | Simple array-based processing in Python or similar languages |
| Sliding Window Method | O(n) | O(1) | When scanning ordered records for fixed-size consecutive segments |
| SQL Window Functions | O(n) | O(n) | Best choice for SQL interviews and relational database queries |
| SQL-like Filter and Sort Strategy | O(n log n) | O(n) | Useful in JavaScript or environments without SQL window functions |