Report consecutive failed and succeeded date ranges in 2019

Quick Overview

Combine a table of failed days and a table of succeeded days, then report each maximal run of consecutive days with the same outcome in 2019 as a start and end date. It tests the gaps-and-islands pattern with window functions, UNION ALL, date arithmetic and grouping in PostgreSQL.

Report consecutive failed and succeeded date ranges in 2019

Company: Databricks

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: hard

Interview Round: Other

One task runs per day and either fails or succeeds. Failed days are recorded in `Failed` and successful days in `Succeeded`. For each maximal interval of consecutive days with the same outcome between `2019-01-01` and `2019-12-31`, return `period_state` (`'failed'` or `'succeeded'`), `start_date` and `end_date`, ordered by `start_date`. ```hint Finding the runs Look for a value you can compute from each date that stays the same across a run of consecutive same-outcome days and changes as soon as the run breaks. ``` ### Input Table(s) `Failed` | Column | Type | Meaning | |---|---|---| | fail_date | DATE | A day on which the task failed | Primary key: `fail_date`. `Succeeded` | Column | Type | Meaning | |---|---|---| | success_date | DATE | A day on which the task succeeded | Primary key: `success_date`. ### Output Contract - Columns, in this order: `period_state` (text, `'failed'` or `'succeeded'`), `start_date` (DATE), `end_date` (DATE). - One row per maximal period. `start_date` and `end_date` are the first and last day of the period, both inclusive. A single isolated day gives a row whose `start_date` equals its `end_date`. - Ordered by `start_date` ascending. Periods never overlap, so `start_date` is unique and the order is fully determined. ### Example Input `Failed`: | fail_date | |---| | 2019-01-01 | | 2019-01-02 | | 2019-01-03 | | 2019-01-06 | | 2019-01-07 | Input `Succeeded`: | success_date | |---| | 2019-01-04 | | 2019-01-05 | | 2019-01-08 | | 2019-01-09 | | 2019-01-10 | Expected result: | period_state | start_date | end_date | |---|---|---| | failed | 2019-01-01 | 2019-01-03 | | succeeded | 2019-01-04 | 2019-01-05 | | failed | 2019-01-06 | 2019-01-07 | | succeeded | 2019-01-08 | 2019-01-10 | Days 2019-01-01 to 2019-01-03 are consecutive failed days, 2019-01-04 to 2019-01-05 are consecutive succeeded days, 2019-01-06 to 2019-01-07 failed again, and 2019-01-08 to 2019-01-10 succeeded. A second, smaller case shows what a missing day does. With `Failed` = 2019-03-01, 2019-03-02, 2019-03-04 and `Succeeded` = 2019-03-05, the result is: | period_state | start_date | end_date | |---|---|---| | failed | 2019-03-01 | 2019-03-02 | | failed | 2019-03-04 | 2019-03-04 | | succeeded | 2019-03-05 | 2019-03-05 | 2019-03-03 has no row in either table, so the failed days on either side of it are not consecutive and form two periods. ### Constraints and Clarifications - No day has more than one event: a date never appears in both tables, and each table has at most one row per date. - The report covers 2019-01-01 through 2019-12-31. Dates outside that range, if present, are ignored, and a period that would extend past either end is cut at the boundary. - Consecutive means calendar-consecutive. A day with no row in either table ends the current period. - Neither table contains NULLs.

Overview: Combine a table of failed days and a table of succeeded days, then report each maximal run of consecutive days with the same outcome in 2019 as a start and end date. It tests the gaps-and-islands pattern with window functions, UNION ALL, date arithmetic and grouping in PostgreSQL.

Databricks logo
Databricks
Nov 24, 2026
hardData ScientistOtherData Manipulation (SQL/Python)
0
0

One task runs per day and either fails or succeeds. Failed days are recorded in Failed and successful days in Succeeded. For each maximal interval of consecutive days with the same outcome between 2019-01-01 and 2019-12-31, return period_state ('failed' or 'succeeded'), start_date and end_date, ordered by start_date.

Input Table(s)

Failed

ColumnTypeMeaning
fail_dateDATEA day on which the task failed

Primary key: fail_date.

Succeeded

ColumnTypeMeaning
success_dateDATEA day on which the task succeeded

Primary key: success_date.

Output Contract

  • Columns, in this order: period_state (text, 'failed' or 'succeeded' ), start_date (DATE), end_date (DATE).
  • One row per maximal period. start_date and end_date are the first and last day of the period, both inclusive. A single isolated day gives a row whose start_date equals its end_date .
  • Ordered by start_date ascending. Periods never overlap, so start_date is unique and the order is fully determined.

Example

Input Failed:

fail_date
2019-01-01
2019-01-02
2019-01-03
2019-01-06
2019-01-07

Input Succeeded:

success_date
2019-01-04
2019-01-05
2019-01-08
2019-01-09
2019-01-10

Expected result:

period_statestart_dateend_date
failed2019-01-012019-01-03
succeeded2019-01-042019-01-05
failed2019-01-062019-01-07
succeeded2019-01-082019-01-10

Days 2019-01-01 to 2019-01-03 are consecutive failed days, 2019-01-04 to 2019-01-05 are consecutive succeeded days, 2019-01-06 to 2019-01-07 failed again, and 2019-01-08 to 2019-01-10 succeeded.

A second, smaller case shows what a missing day does. With Failed = 2019-03-01, 2019-03-02, 2019-03-04 and Succeeded = 2019-03-05, the result is:

period_statestart_dateend_date
failed2019-03-012019-03-02
failed2019-03-042019-03-04
succeeded2019-03-052019-03-05

2019-03-03 has no row in either table, so the failed days on either side of it are not consecutive and form two periods.

Constraints and Clarifications

  • No day has more than one event: a date never appears in both tables, and each table has at most one row per date.
  • The report covers 2019-01-01 through 2019-12-31. Dates outside that range, if present, are ignored, and a period that would extend past either end is cut at the boundary.
  • Consecutive means calendar-consecutive. A day with no row in either table ends the current period.
  • Neither table contains NULLs.