Skip to content
Onsite

Numbers appearing at least three times consecutively

MediumP2Coding

Table logs:

ColumnType
idint (primary key)
numint

Find every number that appears at least three times in a row when the rows are read in id order. Ids are increasing but may skip values, so "in a row" means adjacent rows in id order, not id, id + 1, id + 2.

Output column: consecutive_num. Each number appears once in the result.

Schema

CREATE TABLE logs (
  id int PRIMARY KEY,
  num int NOT NULL
);

Example 1

Input
logs by id: 1, 1, 1, 2, 1, 2, 2
Output
consecutive_num: 1

Example 2

Input
logs by id: 7, 7, 7, 7
Output
consecutive_num: 7
Why
A longer run is reported once.

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 7.