You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].
Example 1:
Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]] Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Example 2:
Input: firstList = [[1,3],[5,9]], secondList = [] Output: []
Constraints:
0 <= firstList.length, secondList.length <= 1000firstList.length + secondList.length >= 10 <= starti < endi <= 109endi < starti+10 <= startj < endj <= 109 endj < startj+1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given two separate schedules of busy times and need to find when both schedules overlap. The brute force approach involves comparing every single time block from the first schedule against every single time block from the second schedule to see if they share any common moments.
Here's how the algorithm would work step-by-step:
def interval_intersection(first_schedule, second_schedule):
overlapping_intervals = []
# Compare every time block from the first schedule against every block in the second schedule
for first_interval in first_schedule:
for second_interval in second_schedule:
# Determine the overlap bounds by finding the latest start time and earliest end time
start_maximum = max(first_interval[0], second_interval[0])
end_minimum = min(first_interval[1], second_interval[1])
# If the start is less than or equal to the end, a valid overlap exists
if start_maximum <= end_minimum:
overlapping_intervals.append([start_maximum, end_minimum])
return overlapping_intervalsTo find the overlapping times between two schedules, we can compare them side by side from beginning to end. By looking at the earliest meetings from both lists simultaneously, we can instantly spot any shared time and naturally advance past the meeting that finishes sooner.
Here's how the algorithm would work step-by-step:
def interval_intersection(first_schedule, second_schedule):
matched_intersections = []
first_index = 0
second_index = 0
while first_index < len(first_schedule) and second_index < len(second_schedule):
first_interval = first_schedule[first_index]
second_interval = second_schedule[second_index]
latest_start = max(first_interval[0], second_interval[0])
earliest_end = min(first_interval[1], second_interval[1])
# An overlap exists only if the latest start time precedes the earliest end time.
if latest_start <= earliest_end:
matched_intersections.append([latest_start, earliest_end])
# Advance the pointer of the interval that finishes first, as it cannot overlap further.
if first_interval[1] < second_interval[1]:
first_index += 1
else:
second_index += 1
return matched_intersections| Case | How to Handle |
|---|---|
| One or both interval lists are empty | The two-pointer loop condition fails immediately, returning an empty result list as expected. |
| Intervals with zero length where start equals end | Point intervals are correctly processed by the overlap logic since closed intervals include single-point intersections. |
| Intervals contain negative numbers | Standard numerical comparisons work correctly for negative ranges without requiring coordinate transformations. |
| Completely disjoint interval lists with no overlaps | The pointer advancement logic correctly skips non-overlapping intervals until one list is fully traversed. |
| One list completely encompasses intervals from the other list | The inner pointer advances only after the fully contained interval is completely compared against the larger enclosing interval. |
| Maximum sized inputs with thousands of intervals | The two-pointer linear scan approach scales efficiently operating in O(N + M) time complexity without recursion overhead. |
| Extreme boundary values reaching integer limits | Using standard 32-bit or 64-bit integers safely handles maximum boundary values without arithmetic overflow during comparisons. |