Report Contiguous Dates - Problem

You're tasked with analyzing the historical performance of a daily task system running throughout 2019. The system executes exactly one independent task each day, which either succeeds or fails.

Given two tables:

  • Failed - Contains dates when tasks failed
  • Succeeded - Contains dates when tasks succeeded

Your goal: Generate a report showing contiguous periods of consecutive days with the same outcome (all failed or all succeeded) from 2019-01-01 to 2019-12-31.

Each period should include:

  • period_state: Either 'failed' or 'succeeded'
  • start_date: First date of the consecutive period
  • end_date: Last date of the consecutive period

Example: If tasks failed on Jan 1-3, succeeded on Jan 4-7, then failed on Jan 8, you'd have three periods: failed (Jan 1-3), succeeded (Jan 4-7), and failed (Jan 8-8).

Input & Output

basic_example.sql โ€” Basic Case
$ Input: Failed table: [2019-01-01, 2019-01-02, 2019-01-03] Succeeded table: [2019-01-04, 2019-01-05]
โ€บ Output: start_date: 2019-01-01, end_date: 2019-01-03, period_state: failed start_date: 2019-01-04, end_date: 2019-01-05, period_state: succeeded
๐Ÿ’ก Note: First 3 days failed consecutively, then 2 days succeeded consecutively, creating two distinct periods
alternating_example.sql โ€” Alternating Status
$ Input: Failed table: [2019-01-01, 2019-01-03, 2019-01-05] Succeeded table: [2019-01-02, 2019-01-04]
โ€บ Output: start_date: 2019-01-01, end_date: 2019-01-01, period_state: failed start_date: 2019-01-02, end_date: 2019-01-02, period_state: succeeded start_date: 2019-01-03, end_date: 2019-01-03, period_state: failed start_date: 2019-01-04, end_date: 2019-01-04, period_state: succeeded start_date: 2019-01-05, end_date: 2019-01-05, period_state: failed
๐Ÿ’ก Note: Status alternates daily, so each day forms its own single-day period
year_boundary.sql โ€” Year Boundary
$ Input: Failed table: [2019-12-30, 2019-12-31] Succeeded table: [2019-12-29]
โ€บ Output: start_date: 2019-12-29, end_date: 2019-12-29, period_state: succeeded start_date: 2019-12-30, end_date: 2019-12-31, period_state: failed
๐Ÿ’ก Note: Tests the year boundary constraint - only dates within 2019 are considered, forming one success period and one failure period

Visualization

Tap to expand
Contiguous Date Period Detection ProcessInput TablesFailed2019-01-012019-01-022019-01-032019-01-08Succeeded2019-01-042019-01-052019-01-06Combined & Sorted01-01 failed (1,1) โ†’ 001-02 failed (2,2) โ†’ 001-03 failed (3,3) โ†’ 001-04 succeeded (4,1) โ†’ 301-05 succeeded (5,2) โ†’ 301-06 succeeded (6,3) โ†’ 301-08 failed (7,4) โ†’ 3Grouping LogicRow Numbering Technique:โ€ข Overall Rank: Sequential number for ALL dates (1,2,3,4,5,6,7...)โ€ข Status Rank: Sequential number WITHIN each status (failed: 1,2,3,4... succeeded: 1,2,3...)โ€ข Group ID = Overall Rank - Status Rank (constant within consecutive periods!)Final ResultStart DateEnd DatePeriod StateGroup ID2019-01-012019-01-03failed02019-01-042019-01-06succeeded32019-01-082019-01-08failed3๐ŸŽฏ Key Insight: The difference (Overall Rank - Status Rank) remains constant within consecutive periods of the same status
Understanding the Visualization
1
Collect All Reports
Gather all success and failure dates from 2019, like collecting daily reports from two different departments
2
Sort Chronologically
Arrange all reports in date order, mixing successes and failures in chronological sequence
3
Number Within Groups
Assign sequential numbers within each status type (1st success, 2nd success, etc.)
4
Calculate Grouping Key
Subtract group numbers from overall sequence - this difference stays constant within consecutive periods
5
Bundle Consecutive Periods
Group by the calculated key to bundle consecutive days of same status together
Key Takeaway
๐ŸŽฏ Key Insight: Using ROW_NUMBER() with the difference technique elegantly groups consecutive dates by exploiting the mathematical property that rank differences remain constant within contiguous sequences of the same status.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting the combined dates is the dominant operation

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space to store the combined and sorted date records

n
2n
โšก Linearithmic Space

Constraints

  • fail_date and success_date are unique within their respective tables
  • All dates are within the range 2019-01-01 to 2019-12-31
  • The system runs exactly one task per day (no missing days in the union)
  • A date cannot appear in both Failed and Succeeded tables
  • Result must be ordered by start_date ascending
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
58.0K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen