Interval patterns: LeetCode & Coding interview guide

Below, I've divided interval problems into five main patterns. Each pattern focuses on a different way of working with intervals, such as merging overlapping ranges, finding intersections, selecting non-overlapping intervals, processing events, or covering a target range. I hope, recognizing these patterns can help you quickly identify the right approach to a problem during coding interview.


Merge Overlapping Intervals

Merge Overlapping Intervals is a pattern used to combine intervals that overlap. The idea is simple: we sort the intervals by their start time, then scan them and merge each overlapping interval with the previous one.


For example, we have an array [[1, 3], [2, 6], [8, 10], [15, 18]].
After sorting by start it is clear that [1, 3] and [2, 6] overlap because 2 <= 3.
After merging this interval ([1, 6]) we have [[1, 6], [8, 10], [15, 18]].


The key check is:

currentStart <= previousEnd

In our example currentStart start is 2, previousEnd is 3.


LeetCode examples:

56. Merge Intervals
57. Insert Interval


Interval Intersection or Two Pointers on Intervals

Interval Intersection is a pattern used to find the common parts of two sorted lists of intervals. We need to use two pointers to compare the current intervals, calculate their overlap, and move the pointer of the interval that ends first.


For example, we have two arrays: a = [[1, 3], [5, 7]] and b = [[2, 4], [6, 8]].
During first comparison a = [1, 3], b = [2, 4].

start = max(1, 2) = 2;
end   = min(3, 4) = 3;

Since start <= end we have 2 <= 3, and the intersection is [2, 3].
Then move the pointer of the interval that ends first. Since 3 < 4 so move pointer a. And so on.
Final result is [[2, 3], [6, 7]].


The key check is:

max(aStart, bStart) <= min(aEnd, bEnd);

// or 

if(aEnd < bEnd)
  i++;
else
  j++;

LeetCode example:

986. Interval List Intersections


Interval Scheduling / Greedy

Interval Scheduling is a greedy pattern used to select the maximum number of non-overlapping intervals or remove the minimum number of overlapping ones. We sort intervals by their end time, then keep the interval that finishes earliest.


For example, we have an array of intervals: [[1, 4], [1, 2], [2, 3]]. After sorting by end time, the array becomes [[1, 2], [2, 3], [1, 4]].
First, keep [1, 2], The next interval is [2, 3]. It does not overlap with [1, 2] because 2 >= 2 (currentStart >= lastEnd), so, we keep it.
Next, consider [1, 4]. It overlaps with the previously selected interval [2, 3] because 1 < 3, so we skip or remove it.
Final selected intervals are [[1, 2], [2, 3]].


Need to remember: sort by end


The key check is:

if(currentStart >= lastEnd)
    keep current interval;

Choosing the interval that ends earliest leaves the most space for future intervals.


LeetCode example:

435. Non-overlapping Intervals


Sweep Line (Events)

Sweep Line is a pattern used to process intervals as a sequence of events and track how many intervals are active at the same time. We convert each interval into a start event and an end event, then all events are processed in sorted order.


For example, we have an array of intervals [[1, 4], [2, 5], [3, 6]].
Convert each interval into two events:

[1, 4] to (1, +1), (4, -1)
[2, 5] to (2, +1), (5, -1)
[3, 6] to (3, +1), (6, -1)

Here, +1 means that an interval starts and -1 means that an interval ends.
Second step - sort the events by position:

(1, +1)
(2, +1)
(3, +1)
(4, -1)
(5, -1)
(6, -1)

Third step - after sorting process the events from left to right:

position 1 to active = 1
position 2 to active = 2
position 3 to active = 3
position 4 to active = 2
position 5 to active = 1
position 6 to active = 0

At the end the maximum number of active intervals is 3.


The key idea of Sweep Line is:

start is active++
end is active--

LeetCode examples:

253. Meeting Rooms II
1854. Maximum Population Year


Interval Coverage

Interval Coverage is a pattern used to determine how a range can be covered by intervals, often with the goal of using the minimum number of intervals. A common greedy approach is to choose the interval that extends the current coverage as far as possible.


For example, we have an array of intervals [[0, 2], [1, 5], [4, 7], [5, 9]], and suppose we need to cover the range [0, 9].

Let's start from 0. Among all intervals that start at or before 0, we choose the one that reaches farthest [0, 2]. Ok, now the covered range is [0, 2]. Among the intervals that start at or before 2, [1, 5] extends the coverage farthest [0, 5].

Next, both [4, 7] and [5, 9] are available. [5, 9] reaches farther, so we choose it - [0, 9].
The entire target range is now covered using [[0, 2], [1, 5], [5, 9]].


The key idea is:


Among all intervals that can extend the current coverage, we choose the one that reaches farthest.


So Interval Coverage is usually a greedy pattern: instead of merging all overlapping intervals, we choose intervals that extend the covered range as far as possible.


LeetCode example:

452. Minimum Number of Arrows to Burst Balloons

© 2026 Algobytes