Table: Logs
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ In SQL, id is the primary key for this table. id is an autoincrement column starting from 1.
Find all numbers that appear at least three times consecutively.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Logs table: +----+-----+ | id | num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ Output: +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+ Explanation: 1 is the only number that appears consecutively for at least three times.
The Consecutive Numbers problem asks you to identify numbers in a log table that appear at least three times in a row. Since this is a database problem, the key idea is to compare rows with their neighboring rows based on the row order (usually defined by an id column).
One common approach is using self-joins. By joining the table with itself multiple times (e.g., comparing rows t1, t2, and t3), you can check whether the same value appears in three consecutive positions. If the values match and the IDs are sequential, that number qualifies as a valid result.
A more modern and cleaner method uses window functions such as LAG() or LEAD(). These functions allow you to access previous or next rows within the ordered dataset, making it easy to verify whether a number repeats consecutively.
Both strategies scan the dataset once or with minimal joins, leading to efficient performance for typical interview datasets. The main goal is correctly identifying consecutive row relationships while avoiding duplicate outputs.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Self Join on Consecutive IDs | O(n) | O(1) |
| Window Functions (LAG/LEAD) | O(n) | O(1) |
NeetCode
We can use a sliding window approach with a counter to track consecutive numbers as we iterate through the list.
Time Complexity: O(n), where n is the number of elements in the list. Space Complexity: O(1).
1function findConsecutiveNumbers(nums) {
2 let result = [];
3 let count = 1;
4 for (let i = 1; i < nums.length; i++) {
5 if (nums[i] === nums[i - 1]) {
6 count++;
7 if (count === 3 && !result.includes(nums[i])) {
8 result.push(nums[i]);
9 }
10 } else {
11 count = 1;
12 }
13 }
14 return result;
15}
16
17const nums = [1, 1, 1, 2, 1, 2, 2];
18console.log(findConsecutiveNumbers(nums));
19For JavaScript, an array is used to keep track of any number that appears at least thrice in a row. The 'count' variable indicates how many consecutive identical numbers have been encountered.
This approach uses a HashMap (or Dictionary in Python) to keep track of the sequence counts of numbers. This way, we can determine if any number is repeated consecutively at least three times.
Time Complexity: O(n). Space Complexity: O(1) since we use only limited extra space.
1
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of this problem can appear in database or SQL interview rounds at large tech companies. It tests your understanding of window functions, joins, and how to analyze ordered data in relational tables.
The problem specifically asks for numbers that appear at least three times in a row. By comparing a row with the previous two or the next two rows, you can confirm whether the value repeats consecutively and qualifies for the output.
A common optimal approach is using SQL window functions like LAG() or LEAD() to compare adjacent rows. These functions make it easy to check whether the same number appears in consecutive positions without complex joins.
Window functions and self-joins are the most useful SQL techniques for this problem. They allow you to compare rows based on order and detect repeated values across consecutive records.
In JavaScript, an object serves the function of a dictionary to prevent redundant consecutive recordings, thereby refining output efficiency when consecutive numbers are detected.