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.

One task runs per day and either fails or succeeds. Failed days are recorded in the `failed` table and successful days in the `succeeded` table. 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 tables: - `failed(fail_date DATE)`: a day on which the task failed. Primary key: `fail_date`. - `succeeded(success_date DATE)`: a day on which the task succeeded. Primary key: `success_date`. Output: - 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. Rules: - 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. Example of a missing day: with `failed` = 2019-03-01, 2019-03-02, 2019-03-04 and `succeeded` = 2019-03-05, the result is three rows: failed 2019-03-01 to 2019-03-02, failed 2019-03-04 to 2019-03-04, and succeeded 2019-03-05 to 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.

Tables

failed(fail_date DATE)

succeeded(success_date DATE)

Hints

  1. 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.
  2. Only days from 2019-01-01 through 2019-12-31 count, so a run that crosses either end of the year is cut at that boundary.
DatabricksData ScientisthardData Manipulation (SQL/Python)

Report Consecutive Failed and Succeeded Task Periods in 2019

One task runs per day and either fails or succeeds. Failed days are recorded in the failed table and successful days in the succeeded table. 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 tables:

  • failed(fail_date DATE): a day on which the task failed. Primary key: fail_date.
  • succeeded(success_date DATE): a day on which the task succeeded. Primary key: success_date.

Output:

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

Rules:

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

Example of a missing day: with failed = 2019-03-01, 2019-03-02, 2019-03-04 and succeeded = 2019-03-05, the result is three rows: failed 2019-03-01 to 2019-03-02, failed 2019-03-04 to 2019-03-04, and succeeded 2019-03-05 to 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.

Expected Output

Tables

failedsucceeded
failed
ColumnType
fail_dateDATE
succeeded
ColumnType
success_dateDATE

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