Quick 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.

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.

Table `logs` holds the entries of a log. | Column | Type | Meaning | |---|---|---| | id | INTEGER | Position of the entry in the log. Primary key. | | num | VARCHAR(20) | The logged number, stored as a string of digits. NOT NULL. | Find all numbers that appear at least three times consecutively (by `id`) in `logs`. Return each such number once, in a single column named `consecutivenums` that holds the `num` value as stored. Writing the alias as `ConsecutiveNums` without quotes gives the same column, because PostgreSQL lower-cases unquoted identifiers. Sort the rows 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 | 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.

Tables

logs(id INTEGER, num VARCHAR(20))

Hints

  1. 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.
  2. A number can meet the rule at more than one position, and each qualifying value should still appear only once in the output.
Goldman SachsData ScientistmediumData Manipulation (SQL/Python)

Consecutive Numbers in a Log

Table logs holds the entries of a log.

ColumnTypeMeaning
idINTEGERPosition of the entry in the log. Primary key.
numVARCHAR(20)The logged number, stored as a string of digits. NOT NULL.

Find all numbers that appear at least three times consecutively (by id) in logs. Return each such number once, in a single column named consecutivenums that holds the num value as stored. Writing the alias as ConsecutiveNums without quotes gives the same column, because PostgreSQL lower-cases unquoted identifiers. Sort the rows 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

idnum
11
21
31
42
51
62
72

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.

Expected Output

Tables

logs
logs
ColumnType
idINTEGER
numVARCHAR(20)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions