Merge Overlapping Events in the Same Hall - Problem

You're managing a venue booking system for a conference center with multiple halls. Each hall can host events, and sometimes bookings overlap or are scheduled back-to-back. Your task is to merge all overlapping events within the same hall to get a clear picture of actual venue usage.

Given a table HallEvents with columns:

  • hall_id (int): The hall identifier
  • start_day (date): Event start date
  • end_day (date): Event end date

Two events overlap if they have at least one day in common. For example, an event from Jan 1-3 overlaps with an event from Jan 3-5 (they share Jan 3).

Your goal: Write a SQL solution to merge all overlapping events in the same hall and return the consolidated booking periods.

Note: The table may contain duplicate rows, and events are only merged if they're in the same hall.

Input & Output

example_1.sql โ€” Basic Overlap
$ Input: HallEvents table: +----------+------------+----------+ | hall_id | start_day | end_day | +----------+------------+----------+ | 1 | 2023-01-13 | 2023-01-15 | | 1 | 2023-01-14 | 2023-01-17 | | 1 | 2023-01-18 | 2023-01-25 | | 2 | 2023-01-10 | 2023-01-20 | | 2 | 2023-01-15 | 2023-01-17 | +----------+------------+----------+
โ€บ Output: +----------+------------+----------+ | hall_id | start_day | end_day | +----------+------------+----------+ | 1 | 2023-01-13 | 2023-01-17 | | 1 | 2023-01-18 | 2023-01-25 | | 2 | 2023-01-10 | 2023-01-20 | +----------+------------+----------+
๐Ÿ’ก Note: In hall 1: events Jan 13-15 and Jan 14-17 overlap (share Jan 14-15), so they merge to Jan 13-17. Event Jan 18-25 is separate. In hall 2: event Jan 15-17 is completely contained within Jan 10-20, so they merge to Jan 10-20.
example_2.sql โ€” Adjacent Events
$ Input: HallEvents table: +----------+------------+----------+ | hall_id | start_day | end_day | +----------+------------+----------+ | 1 | 2023-01-01 | 2023-01-03 | | 1 | 2023-01-03 | 2023-01-05 | | 1 | 2023-01-06 | 2023-01-08 | +----------+------------+----------+
โ€บ Output: +----------+------------+----------+ | hall_id | start_day | end_day | +----------+------------+----------+ | 1 | 2023-01-01 | 2023-01-05 | | 1 | 2023-01-06 | 2023-01-08 | +----------+------------+----------+
๐Ÿ’ก Note: Events Jan 1-3 and Jan 3-5 overlap on Jan 3, so they merge to Jan 1-5. Event Jan 6-8 doesn't overlap with the merged event (no shared days), so it remains separate.
example_3.sql โ€” Duplicate Events
$ Input: HallEvents table: +----------+------------+----------+ | hall_id | start_day | end_day | +----------+------------+----------+ | 1 | 2023-01-01 | 2023-01-03 | | 1 | 2023-01-01 | 2023-01-03 | | 1 | 2023-01-02 | 2023-01-04 | +----------+------------+----------+
โ€บ Output: +----------+------------+----------+ | hall_id | start_day | end_day | +----------+------------+----------+ | 1 | 2023-01-01 | 2023-01-04 | +----------+------------+----------+
๐Ÿ’ก Note: All three events (including duplicates) overlap and merge into a single period from Jan 1-4. The duplicate Jan 1-3 events are handled naturally by the DISTINCT clause.

Constraints

  • 1 โ‰ค Number of rows โ‰ค 104
  • 1 โ‰ค hall_id โ‰ค 100
  • start_day โ‰ค end_day
  • Table may contain duplicate rows
  • Events overlap if they share at least one day

Visualization

Tap to expand
Merging Overlapping EventsHall 1 - Before:Jan 1-5Jan 3-8Jan 6-9Hall 1 - After:Jan 1-9 (Merged)Hall 2 - Before:Jan 10-20Jan 15-17Hall 2 - After:Jan 10-20๐ŸŽฏ Key Insight: Use window functions to identify island boundaries
Understanding the Visualization
1
Sort by Hall & Start
Organize events chronologically within each hall
2
Detect Gaps
Find where current event starts after previous event ends
3
Create Islands
Group consecutive overlapping events together
4
Merge Islands
Combine each island into a single time period
Key Takeaway
๐ŸŽฏ Key Insight: The islands and gaps technique efficiently groups overlapping intervals using window functions, avoiding the need for expensive self-joins.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
52.0K Views
High Frequency
~25 min Avg. Time
1.8K 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