Consecutive Numbers - Problem
Database Query Challenge: Finding Consecutive Patterns
You are given a database table called
Table Schema:
โข
โข
Goal: Write a SQL query to find all unique numbers that appear in three or more consecutive rows. The order of results doesn't matter.
Example:
If the Logs table contains: [1,1,1,2,1,1,2,2], then number
You are given a database table called
Logs that contains numerical data with sequential IDs. Your task is to identify all numbers that appear at least three times consecutively in the table.Table Schema:
Logs table:โข
id (int): Primary key, auto-increment starting from 1โข
num (varchar): The number value to analyzeGoal: Write a SQL query to find all unique numbers that appear in three or more consecutive rows. The order of results doesn't matter.
Example:
If the Logs table contains: [1,1,1,2,1,1,2,2], then number
1 appears three times consecutively (rows 1-3), so it should be included in the result. Input & Output
example_1.sql โ Basic Consecutive Pattern
$
Input:
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
โบ
Output:
Result table:
+-----------------+
| ConsecutiveNum |
+-----------------+
| 1 |
+-----------------+
๐ก Note:
Number '1' appears in rows 1, 2, and 3 consecutively, forming a streak of exactly 3. Number '2' only appears consecutively in rows 6-7 (streak of 2), which doesn't meet our requirement of at least 3.
example_2.sql โ Multiple Consecutive Patterns
$
Input:
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
โบ
Output:
Result table:
+-----------------+
| ConsecutiveNum |
+-----------------+
| 1 |
| 2 |
+-----------------+
๐ก Note:
Both numbers qualify: '1' appears consecutively in rows 1-3 (streak of 3), and '2' appears consecutively in rows 4-7 (streak of 4). Both meet the minimum requirement of 3 consecutive appearances.
example_3.sql โ No Consecutive Pattern
$
Input:
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
+----+-----+
โบ
Output:
Result table:
+-----------------+
| ConsecutiveNum |
+-----------------+
๐ก Note:
No number appears 3 times consecutively. The pattern alternates between 1 and 2, so the maximum consecutive count for any number is 1. The result table is empty.
Visualization
Tap to expand
Understanding the Visualization
1
Position Window
Place a 3-element window starting at the first row
2
Compare Values
Check if all three values in the window are identical
3
Slide Window
Move window one position forward and repeat
4
Collect Results
Record any number that creates a 3+ consecutive match
Key Takeaway
๐ฏ Key Insight: Window functions eliminate the need for expensive self-joins by allowing us to access neighboring rows directly, making consecutive pattern detection both elegant and efficient.
Time & Space Complexity
Time Complexity
O(n)
Single pass through the table with window function
โ Linear Growth
Space Complexity
O(1)
Window functions use constant extra space
โ Linear Space
Constraints
- 1 โค Logs table size โค 104
-
idis a unique auto-increment integer starting from 1 -
numis stored as varchar but contains numeric values - Consecutive means the IDs must be sequential (no gaps)
- Result should contain unique numbers only (use DISTINCT)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code