Interval List Intersections
Keep i in the first list and j in the second. Compute lo = max(starts) and hi = min(ends). If lo ≤ hi, add [lo, hi]. Then move past the interval that ends first. It can't overlap anything else, because everything later in the other list starts after the other current interval ends.
Two closed intervals overlap exactly when lo = max(starts) ≤ hi = min(ends), and then the overlap is [lo, hi].
Problem
You get two lists of closed intervals, firstList and secondList. A closed interval [a, b] means every number from a to b, including both ends.
Inside each list, the intervals are sorted and don't overlap or touch each other.
Return every place where the two lists overlap, as a list of intervals in order. If two intervals only share one point, that point counts as an interval [x, x].
Two lists of intervals firstList and secondList, where each interval is [start, end].
A list of intervals: the intersection of the two lists, sorted by start. It can be empty.
[0,2] and [1,5] share [1,2]. [5,10] and [1,5] share only the point 5, so [5,5]. [24,25] and [15,24] share only 24, and [24,25] and [25,26] share only 25.
The second list is empty, so nothing overlaps.
- • 0 ≤ firstList.length, secondList.length ≤ 1000
- • firstList.length + secondList.length ≥ 1
- • 0 ≤ start < end ≤ 10^9
- • Within one list: end of one interval < start of the next
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Given just two intervals, how do I find the part they share? When do they share nothing?
- 2.
Is touching at one point an overlap here? What does "closed" tell me?
- 3.
Both lists are sorted. Does that remind me of another problem where I walk two sorted lists at once?
- 4.
After comparing one interval from each list, which of the two can never overlap anything else?
- 5.
How many comparisons do I really need if I never go backwards?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Compare every interval of the first list with every interval of the second | O(M · N) | O(1) |
| Optimal | Walk both lists together; output the overlap, then advance the interval that ends first | O(M + N) | O(1) |
Why does this belong to this pattern?
- “two lists, each sorted and disjoint”Walk both lists together with one pointer each, like merging two sorted arrays.
- “intersection”The overlap of two intervals is [max of starts, min of ends], if that range isn't empty.
- “closed intervals”Ends are included, so the test is lo ≤ hi and a single point [x, x] counts.
This is the anchor of the sub-pattern. Unlike Merge Intervals, we don't combine intervals from one list. We look at two lists and keep only the time that both cover. The whole idea is one rule: output the overlap, then advance the one that ends first. Meeting Scheduler and Employee Free Time reuse this walk.
Don't make this mistake
The interval that ends later may still overlap the next interval of the other list. With [[0,10]] and [[1,2],[5,6]], moving both after the first overlap loses [5,6].
The interval with the larger end is the one that can still reach more of the other list. Drop the one that ends first, because it is the one that is finished.
The intervals are closed, so [5,10] and [1,5] both include time 5. The answer must include [5,5].
This is not Merge Intervals. Putting both lists together, sorting and merging gives the union (time covered by either list). We want the time covered by both. Also, sorting would waste the fact that both lists are already sorted. Two pointers do it in O(M + N).
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.