Human Traffic of Stadium - Problem
๐๏ธ Stadium Traffic Analysis
You're managing a stadium's visitor analytics system. Given a database table containing daily visitor records, you need to identify periods of sustained high traffic - specifically, any sequence of three or more consecutive days where each day had at least 100 visitors.
The Stadium table contains:
id- Unique identifier (auto-incrementing with date)visit_date- Date of visitpeople- Number of visitors that day
Goal: Find all records that are part of a streak of 3+ consecutive days with โฅ100 visitors each day.
Output: Return matching records ordered by visit_date in ascending order.
Input & Output
example_1.sql โ Basic Consecutive Sequence
$
Input:
Stadium:
| 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-08 | 188 |
โบ
Output:
| id | visit_date | people |
|----|------------|--------|
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
๐ก Note:
Records with IDs 5,6,7,8 form a consecutive sequence of 4 days, each with โฅ100 people. Records 2,3 only form a sequence of 2 days (not enough), and other records don't meet the people threshold.
example_2.sql โ Multiple Valid Sequences
$
Input:
Stadium:
| id | visit_date | people |
|----|------------|--------|
| 1 | 2017-01-01 | 150 |
| 2 | 2017-01-02 | 200 |
| 3 | 2017-01-03 | 180 |
| 4 | 2017-01-04 | 50 |
| 5 | 2017-01-05 | 120 |
| 6 | 2017-01-06 | 130 |
| 7 | 2017-01-07 | 140 |
โบ
Output:
| id | visit_date | people |
|----|------------|--------|
| 1 | 2017-01-01 | 150 |
| 2 | 2017-01-02 | 200 |
| 3 | 2017-01-03 | 180 |
| 5 | 2017-01-05 | 120 |
| 6 | 2017-01-06 | 130 |
| 7 | 2017-01-07 | 140 |
๐ก Note:
Two separate consecutive sequences: IDs 1,2,3 (3 days) and IDs 5,6,7 (3 days). ID 4 breaks the sequence with only 50 people.
example_3.sql โ No Valid Sequences
$
Input:
Stadium:
| id | visit_date | people |
|----|------------|--------|
| 1 | 2017-01-01 | 150 |
| 2 | 2017-01-02 | 200 |
| 3 | 2017-01-03 | 50 |
| 4 | 2017-01-04 | 180 |
| 5 | 2017-01-05 | 120 |
โบ
Output:
| id | visit_date | people |
|----|------------|--------|
(empty result)
๐ก Note:
No consecutive sequence of 3+ days exists where all days have โฅ100 people. The longest valid sequences are only 2 days (IDs 1,2 and IDs 4,5).
Visualization
Tap to expand
Understanding the Visualization
1
Filter High Traffic
First, we filter out days with low attendance (< 100 people)
2
Detect Patterns
Use the mathematical property: consecutive IDs have constant (ID - ROW_NUMBER) when sorted
3
Group & Validate
Group records with same (ID - ROW_NUMBER) value and keep groups with 3+ records
4
Return Results
Output all records from valid groups, sorted by date
Key Takeaway
๐ฏ Key Insight: The difference between ID and ROW_NUMBER remains constant for consecutive records, allowing us to group them efficiently without complex joins!
Time & Space Complexity
Time Complexity
O(n log n)
Single scan with sorting for window function, much more efficient than joins
โก Linearithmic
Space Complexity
O(n)
Linear space for storing intermediate grouping results
โก Linearithmic Space
Constraints
- 1 โค Stadium table rows โค 104
- 1 โค id โค 104
- people โฅ 0
- visit_date is a valid date
- id increases with visit_date (chronological order)
- All visit_dates are unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code