Find Numbers That Appear at Least Three Times Consecutively in a Log Table
Company: Goldman Sachs
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Find all numbers that appear at least three times consecutively (by `id`) in `Logs`. Return each such number once, in a column named `ConsecutiveNums`, ordered by `ConsecutiveNums` ascending.
```hint Line the rows up
A run of three is three rows with neighboring ids. Find a way to put each row's `num` next to the `num` of the two rows after it.
```
### Input Table(s)
Table `Logs`
| Column | Type | Meaning |
|---|---|---|
| id | INT | Position of the entry in the log. Primary key. |
| num | VARCHAR(20) | The logged number, stored as a string of digits. NOT NULL. |
Primary key: `id`.
### Output Contract
- Exactly one column, named `ConsecutiveNums` (case as written), holding the `num` value as stored.
- One row per qualifying number, with no repeats.
- Rows sorted by `ConsecutiveNums` ascending. The column is text, so this is string order: `'10'` sorts before `'9'`. Each value appears once, so the order is unique.
### Example
`Logs`
| id | num |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
Expected result
| ConsecutiveNums |
|---|
| 1 |
The number 1 appears three times in a row (ids 1, 2, 3), so it is included. No other number appears three times consecutively: 2 shows up three times in total, but its longest run is ids 6 and 7.
### Constraints and Clarifications
- The table has 1 to 100,000 rows.
- `id` starts at 1 and increments by 1 with no gaps, so consecutive rows are rows with consecutive `id` values.
- `num` is never NULL and holds only digits. Two rows match only when their `num` strings are equal as stored.
- A number whose run is longer than three, or that has several separate runs of three or more, is still returned once.
- If no number has a run of three, the result is empty.
Overview: A SQL problem on a log table: return every number that shows up in at least three consecutive rows by id, listing each number once in sorted order. It tests comparing a row with the rows that follow it, telling runs apart from total counts, and removing repeated matches.