Watch 10 video solutions for Consecutive Numbers, a medium level problem involving Database. This walkthrough by NeetCode has 455,447 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Problem Overview: You are given a table or sequence of logged numbers. The task is to return all values that appear at least three times consecutively. Order matters: the same number must appear in three adjacent records, not just three total occurrences.
Approach 1: Sliding Window Using Array Traversal (O(n) time, O(1) space)
Traverse the ordered records and examine a window of three consecutive positions at a time. For index i, compare nums[i], nums[i-1], and nums[i-2]. If all three values match, you found a valid consecutive sequence. This works because a run of three identical adjacent values guarantees the requirement. The algorithm performs a single linear scan and only stores the result set, so space complexity stays constant. This approach is conceptually identical to a sliding window over a fixed window size of three.
In SQL-based versions of the problem, the same logic is implemented using self joins or window functions like LAG() and LEAD() to compare neighboring rows. In imperative languages such as Python, Java, or C++, you simply iterate through the array and compare neighbors directly.
Approach 2: HashMap / Dictionary to Count Consecutive Streaks (O(n) time, O(n) space)
Track consecutive streaks while iterating through the sequence. Maintain a dictionary where the current number maps to its ongoing streak length. When the current value equals the previous value, increment the streak; otherwise reset the streak to 1. Once the streak reaches 3, record that number in the result. The hash map allows flexible tracking if the dataset is processed as a stream or if multiple sequences must be monitored simultaneously. Lookups and updates run in constant time using a hash map.
This approach is slightly more general than the sliding window technique because it explicitly tracks streak lengths rather than checking a fixed window. It is useful when the problem changes to "k consecutive occurrences" instead of exactly three.
Recommended for interviews: The sliding window traversal is the most expected solution. It shows you recognize that only adjacent comparisons are required and that a constant-size window solves the problem in O(n) time and O(1) space. Mentioning the hash map streak approach demonstrates deeper understanding and adaptability if the requirement changes to variable-length sequences. Both approaches are common patterns when working with ordered datasets or database logs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window Using Array Traversal | O(n) | O(1) | Best general solution when data is ordered and you only need to check fixed-length consecutive values |
| HashMap / Dictionary Streak Tracking | O(n) | O(n) | Useful when tracking variable-length sequences or processing streaming data |