Explain the gaps and islands pattern: finding consecutive runs and breaks with SQL window functions

Quick Overview

Asks how to group consecutive rows of a SQL table into runs (islands) and find the breaks between them (gaps), and which window functions do the work. Tests partitioning and ordering logic, building a group key, and edge cases such as duplicate rows and timestamps.

Explain the gaps and islands pattern: finding consecutive runs and breaks with SQL window functions

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the gaps and islands problem pattern. How do you identify consecutive sequences (islands) or the breaks between them (gaps), and what window functions are commonly used to solve it? A typical setting is an activity table with one row per user per active day, where you want each user's streaks of consecutive days. ```hint What stays constant Within an unbroken run, look for a value you can compute on every row that is the same for the whole run and changes as soon as the run breaks. ``` ```hint Compare with the neighbor You can also compare each row with the previous row of the same entity and mark where a new run starts. Think about how to turn those marks into a group id. ``` ### What a Strong Answer Covers - A clear definition of islands (maximal consecutive runs) and gaps (the missing stretches between them), computed per entity - At least one way to build an island key, and why it stays constant inside a run - The window functions involved and the role of `PARTITION BY` and `ORDER BY` in each - The final aggregation into each run's start, end and length, and how to list the gaps themselves - Pitfalls such as duplicate rows, timestamps or non-unit steps, and ties in the ordering ### Follow-up Questions - How would you return each user's longest streak, and their current streak as of today? - How would you change the query so that one missing day does not break a streak? - How would you group raw event timestamps into sessions that end after 30 minutes of inactivity? - What indexes or data layout help this query on a table with billions of rows?

Overview: Asks how to group consecutive rows of a SQL table into runs (islands) and find the breaks between them (gaps), and which window functions do the work. Tests partitioning and ordering logic, building a group key, and edge cases such as duplicate rows and timestamps.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the gaps and islands problem pattern. How do you identify consecutive sequences (islands) or the breaks between them (gaps), and what window functions are commonly used to solve it?

A typical setting is an activity table with one row per user per active day, where you want each user's streaks of consecutive days.

What a Strong Answer Covers Guidance

  • A clear definition of islands (maximal consecutive runs) and gaps (the missing stretches between them), computed per entity
  • At least one way to build an island key, and why it stays constant inside a run
  • The window functions involved and the role of PARTITION BY and ORDER BY in each
  • The final aggregation into each run's start, end and length, and how to list the gaps themselves
  • Pitfalls such as duplicate rows, timestamps or non-unit steps, and ties in the ordering

Follow-up Questions Guidance

  • How would you return each user's longest streak, and their current streak as of today?
  • How would you change the query so that one missing day does not break a streak?
  • How would you group raw event timestamps into sessions that end after 30 minutes of inactivity?
  • What indexes or data layout help this query on a table with billions of rows?