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 visit
  • people - 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
Stadium Traffic AnalysisRaw DataDay 1: 50 people โŒDay 2: 150 people โœ“Day 3: 200 people โœ“Day 4: 180 people โœ“Day 5: 90 people โŒFiltered (โ‰ฅ100)ID 2: 150 peopleID 3: 200 peopleID 4: 180 peopleConsecutive: 2โ†’3โ†’4Group AnalysisID 2 - ROW 1 = 1ID 3 - ROW 2 = 1ID 4 - ROW 3 = 1Same group key = 1ResultGroup 1: 3 records3 โ‰ฅ 3 โœ“Valid sequence!Return all 3 recordsMagic Formula: (ID - ROW_NUMBER) Groups Consecutive Records!Why This Worksโ€ข Consecutive IDs: 5,6,7,8 with ROW_NUMBERS: 1,2,3,4โ€ข Differences: (5-1=4), (6-2=4), (7-3=4), (8-4=4) โ†’ Same group!
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Linear space for storing intermediate grouping results

n
2n
โšก 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
Asked in
Facebook 35 Google 28 Amazon 22 Microsoft 18
21.0K Views
High Frequency
~25 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