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 log of numbers where each row has an id and a num. The goal is to return all values of num that appear at least three times consecutively in the ordered log. Consecutive means the same number appears in three adjacent rows.
Approach 1: Sliding Window Using Array Traversal (Time: O(n), Space: O(1))
This approach scans the sequence once while checking groups of three adjacent elements. Treat the dataset like an ordered array and compare nums[i], nums[i+1], and nums[i+2]. If all three are equal, record the number as a valid result. The key insight is that any sequence of three consecutive identical numbers must appear in at least one such window. This is essentially a fixed-size sliding window of length 3 moving through the dataset. Because each element is visited once and comparisons are constant time, the runtime is O(n) with O(1) additional memory.
Approach 2: HashMap / Dictionary to Count Occurrences and Track Sequences (Time: O(n), Space: O(n))
This method keeps track of the current streak of each number while iterating through the log. Maintain a dictionary that records how many consecutive times the current value has appeared. When the same value continues from the previous row, increment its streak; otherwise reset the count. Once a streak reaches three, add that number to the result set. A hash-based structure such as a hash map provides constant-time updates and lookups. The algorithm still processes each record once, giving O(n) time complexity, while the dictionary introduces O(n) worst‑case space if many distinct values appear.
In database environments, the same idea is often implemented using row comparisons or window functions. The logic mirrors the traversal approach: compare each row with its neighbors and detect runs of identical values. These techniques commonly appear in database interview questions where identifying ordered patterns is required.
Recommended for interviews: The sliding window traversal is the expected solution. It demonstrates that you recognize the pattern of checking fixed-length consecutive elements and can achieve O(n) time with constant memory. Explaining a more general counting or hash map method first can show your reasoning process, but the constant-space sliding window is the cleanest and most efficient approach.
We can use a sliding window approach with a counter to track consecutive numbers as we iterate through the list.
In C, we traverse the array while maintaining a counter to track consecutive identical numbers. If the sequence reaches three and continues consecutively, the number is printed and the function terminates.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of elements in the list. Space Complexity: O(1).
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.
In C, we use an auxiliary array to mimic a hashmap for counting occurrences of numbers. We focus on consecutive occurrences rather than entire counts, thus determining which numbers are consecutive.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n). Space Complexity: O(1) since we use only limited extra space.
| Approach | Complexity |
|---|---|
| Sliding Window Using Array Traversal | Time Complexity: O(n), where n is the number of elements in the list. Space Complexity: O(1). |
| HashMap/Dictionary to Count Occurrences and Track Sequences | Time Complexity: O(n). Space Complexity: O(1) since we use only limited extra space. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window Using Array Traversal | O(n) | O(1) | Best choice when scanning ordered logs and checking fixed-length consecutive values |
| HashMap / Dictionary to Track Consecutive Counts | O(n) | O(n) | Useful when tracking streaks dynamically or when extending to variable-length sequences |
Leetcode 128 - LONGEST CONSECUTIVE SEQUENCE • NeetCode • 455,447 views views
Watch 9 more video solutions →Practice Consecutive Numbers with our built-in code editor and test cases.
Practice on FleetCode