The Consecutive Numbers interview question is a medium-level SQL problem. You are given a Logs table with an Id column and a Num column. Your goal is to find all numbers that appear at least three times consecutively. This means the same number must appear in three rows with consecutive Id values.
Companies like Microsoft and Uber use the Consecutive Numbers coding problem to test a candidate's ability to perform analytical queries. It requires understanding how to look across multiple rows to identify trends, which is a core skill for data engineers and backend developers working with event logs or audit trails.
This follows the Database interview pattern. There are two popular ways to solve this:
Suppose your log table looks like this:
In this case, only the number 1 appears three times in a row (IDs 1, 2, and 3). Number 2 appears twice at the end, but that doesn't meet the "at least three" requirement.
Be ready to explain the trade-offs between a JOIN approach and a Window Function approach. Window functions are generally preferred in modern SQL for their efficiency and readability.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Monthly Transactions I | Medium | Solve | |
| Last Person to Fit in the Bus | Medium | Solve | |
| Confirmation Rate | Medium | Solve | |
| Product Sales Analysis III | Medium | Solve | |
| Rank Scores | Medium | Solve |