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 failedSucceeded- 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 periodend_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
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
โก Linearithmic
Space Complexity
O(n)
Space to store the combined and sorted date records
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code