Reviewed by Aditya Kumar · Last reviewed 2026-08-08
To find numbers appearing at least three times consecutively, utilize SQL window functions to create a unique group identifier for each sequence of identical, consecutive numbers. Then, filter these…
This medium-level General/Other question appears frequently in data engineering interviews at companies like Meesho. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (partition) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
To find numbers appearing at least three times consecutively, utilize SQL window functions to create a unique group identifier for each sequence of identical, consecutive numbers. Then, filter these groups based on their count.
The core idea is to leverage the difference between two ROW_NUMBER() calculations. Assume an id column exists to define the order of "consecutive" entries.
ROW_NUMBER() OVER (ORDER BY id) assigns a sequential number to each row in the entire dataset based on the id column.ROW_NUMBER() OVER (PARTITION BY num ORDER BY id) assigns a sequential number to each row within each partition of identical num values, also ordered by id.When you subtract the partitioned row number from the overall row number (ROW_NUMBER() OVER (ORDER BY id) - ROW_NUMBER() OVER (PARTITION BY num ORDER BY id)), a constant grp value is generated for all rows that are part of the same consecutive sequence of num. This is because for a consecutive block of identical numbers, both ROW_NUMBER() functions increment in lockstep, maintaining a constant difference. When the num changes, the ROW_NUMBER() OVER (PARTITION BY num ...) resets, causing the difference to change, thus creating a new grp for the next consecutive block.
WITH NumberGroups AS (
SELECT
id,
num,
(ROW_NUMBER() OVER (ORDER BY id) - ROW_NUMBER() OVER (PARTITION BY num ORDER BY id)) AS grp
FROM
Logs -- Assuming 'Logs' is your table and 'id' defines order
)
SELECT DISTINCT num
FROM NumberGroups
GROUP BY num, grp
HAVING COUNT(*) >= 3;
This approach is generally efficient for moderate datasets. For very large datasets, window functions, especially those with PARTITION BY and ORDER BY clauses, can be computationally intensive. They often require significant data shuffling (e.g., in distributed systems like Spark) and sorting, which can impact performance. The ORDER BY id clause is crucial for defining "consecutive" and can be a bottleneck if the ordering column is not indexed or if the data is highly unsorted.
Emphasize the critical role of an ordering column (e.g., id, timestamp) to properly define "consecutive." Discuss the performance implications of window functions on large-scale data and potential optimizations like ensuring appropriate indexing or understanding data distribution to minimize shuffle.
Pro-Move: This is a classic SQL puzzle—knowing the row_number difference trick shows pattern recognition.
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked General/Other interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.