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).
1#include <iostream>
2#include <vector>
3using namespace std;
4
5vector<int> findConsecutiveNumbers(vector<int> nums) {
6 vector<int> result;
7 int count = 1;
8 for (size_t i = 1; i < nums.size(); i++) {
9 if (nums[i] == nums[i - 1]) {
10 count++;
11 if (count == 3) result.push_back(nums[i]);
12 } else {
13 count = 1;
14 }
15 }
16 return result;
17}
18
19int main() {
20 vector<int> nums = {1, 1, 1, 2, 1, 2, 2};
21 vector<int> result = findConsecutiveNumbers(nums);
22 for (int num : result) cout << num << endl;
23 return 0;
24}
25In C++, we use a vector to store our numbers and a similar counter logic to the C solution. Once three consecutive numbers are found, they are added to the result vector.
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.
1using System;
using System.Collections.Generic;
class Program {
static List<int> FindConsecutiveNumbers(int[] nums) {
var count = new Dictionary<int, bool>();
var result = new List<int>();
int consecutive = 1;
for (int i = 1; i < nums.Length; i++) {
if (nums[i] == nums[i - 1]) {
consecutive++;
if (consecutive == 3 && !count.ContainsKey(nums[i])) {
result.Add(nums[i]);
count[nums[i]] = true;
}
} else {
consecutive = 1;
}
}
return result;
}
static void Main() {
int[] nums = {1, 1, 1, 2, 1, 2, 2};
var result = FindConsecutiveNumbers(nums);
foreach (var num in result) {
Console.WriteLine(num);
}
}
}
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 C#, the dictionary approach is utilized to ensure numbers are recorded once they occur consecutively three or more times, preventing duplicate outputs.