Table: HallEvents
+-------------+------+ | Column Name | Type | +-------------+------+ | hall_id | int | | start_day | date | | end_day | date | +-------------+------+ This table may contain duplicates rows. Each row of this table indicates the start day and end day of an event and the hall in which the event is held.
Write a solution to merge all the overlapping events that are held in the same hall. Two events overlap if they have at least one day in common.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: HallEvents table: +---------+------------+------------+ | hall_id | start_day | end_day | +---------+------------+------------+ | 1 | 2023-01-13 | 2023-01-14 | | 1 | 2023-01-14 | 2023-01-17 | | 1 | 2023-01-18 | 2023-01-25 | | 2 | 2022-12-09 | 2022-12-23 | | 2 | 2022-12-13 | 2022-12-17 | | 3 | 2022-12-01 | 2023-01-30 | +---------+------------+------------+ Output: +---------+------------+------------+ | hall_id | start_day | end_day | +---------+------------+------------+ | 1 | 2023-01-13 | 2023-01-17 | | 1 | 2023-01-18 | 2023-01-25 | | 2 | 2022-12-09 | 2022-12-23 | | 3 | 2022-12-01 | 2023-01-30 | +---------+------------+------------+ Explanation: There are three halls. Hall 1: - The two events ["2023-01-13", "2023-01-14"] and ["2023-01-14", "2023-01-17"] overlap. We merge them in one event ["2023-01-13", "2023-01-17"]. - The event ["2023-01-18", "2023-01-25"] does not overlap with any other event, so we leave it as it is. Hall 2: - The two events ["2022-12-09", "2022-12-23"] and ["2022-12-13", "2022-12-17"] overlap. We merge them in one event ["2022-12-09", "2022-12-23"]. Hall 3: - The hall has only one event, so we return it. Note that we only consider the events of each hall separately.
Problem Overview: You are given event records for halls where each event has a start and end day. Some events in the same hall overlap or touch each other. The goal is to merge those overlapping intervals and return the consolidated date ranges per hall.
Approach 1: Self-Join Interval Expansion (O(n²) time, O(n) space)
The most direct SQL approach compares every interval with other intervals in the same hall using a self join. Two events overlap when start_day <= other_end_day and end_day >= other_start_day. By repeatedly merging these ranges with MIN(start_day) and MAX(end_day), you can collapse overlapping intervals into larger blocks. This works but becomes expensive because every row potentially compares with many others. With large event tables the quadratic comparisons make it impractical.
Approach 2: Gaps and Islands with Window Functions (O(n log n) time, O(n) space)
The efficient solution treats overlapping intervals as an island detection problem. First sort events by hall_id and start_day. Then use a window function like LAG() or a running MAX(end_day) to detect when a new interval no longer overlaps the previous merged range. Whenever start_day is greater than the running maximum end, a new group begins. Assign a group id using cumulative sums, then aggregate each group with MIN(start_day) and MAX(end_day). This is the classic SQL gaps-and-islands technique and scales well since the database only performs sorting and linear window scans.
Window functions make the logic concise and performant. MySQL computes the running comparisons in a single pass after sorting, avoiding expensive cross joins. If you frequently solve interval merging problems in SQL, mastering this pattern is essential.
Conceptually this is similar to interval merging problems in algorithm interviews, but implemented using SQL analytics instead of arrays. The same pattern appears in scheduling systems, calendar consolidation, and booking platforms.
Recommended for interviews: The window-function gaps-and-islands approach is what interviewers expect. Mentioning the naive self-join shows you understand interval overlap detection, but using analytic functions demonstrates strong SQL skills and scales to large datasets. Related patterns appear frequently in database, SQL, and window function problems.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Self-Join Interval Merge | O(n²) | O(n) | Small datasets or when window functions are unavailable |
| Window Function Gaps and Islands | O(n log n) | O(n) | General case in modern SQL engines like MySQL 8+, PostgreSQL, SQL Server |
| Recursive CTE Interval Merge | O(n log n) | O(n) | Useful when building intervals step‑by‑step or when window functions are limited |
Amazon Data Engineer SQL Interview Problem | Leetcode Hard SQL 2494 | Recursive CTE • Ankit Bansal • 23,363 views views
Watch 1 more video solutions →Practice Merge Overlapping Events in the Same Hall with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor