Interval problems are the quiet workhorses of coding interviews. Calendar scheduling, meeting rooms, CPU task windows, flight bookings — whenever a problem hands you pairs of start and end times, you are almost certainly looking at the Merge Intervals pattern. It appears constantly in Amazon, Google, and Microsoft rounds, and unlike harder graph patterns, it can be mastered in a weekend. Yet candidates still fumble it, because the difficulty is not the algorithm — it is the edge cases.
This guide covers the signals that tell you to reach for this pattern, the sorting insight that powers every variant, a clean Java template, and the five classic problems that cover nearly every interval question you will face in 2026.

What Is the Merge Intervals Pattern?
An interval is simply a pair [start, end]. The pattern deals with questions about how a collection of intervals relate to each other: do they overlap, can they be merged, how many are simultaneously active, and what gaps exist between them.
The core insight is that once intervals are sorted by start time, all decisions become local. After sorting, an interval can only overlap with the one merged region immediately before it — never with something far back in the list. That single observation collapses a messy O(n²) comparison problem into one sort plus one linear pass: O(n log n) total.
When to Reach for This Pattern
Watch for these signals in the problem statement: explicit pairs like meeting times, job schedules, or booking windows; verbs like “merge,” “overlap,” “conflict,” or “free time”; questions asking for a minimum number of rooms, platforms, or resources; or a request to insert a new range into an existing schedule. If two of those signals appear together, sort by start time and start merging — say that decision out loud in the interview, because naming the pattern early is a strong signal to your interviewer.

The Java Template to Memorize
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
List<int[]> merged = new ArrayList<>();
for (int[] cur : intervals) {
int[] last = merged.isEmpty() ? null : merged.get(merged.size() - 1);
if (last != null && cur[0] <= last[1]) {
last[1] = Math.max(last[1], cur[1]); // overlap: extend
} else {
merged.add(cur); // no overlap: start new region
}
}
return merged.toArray(new int[0][]);
}
Two details in that template decide whether you pass the round. First, the overlap check is cur[0] <= last[1] — whether touching intervals like [1,4] and [4,5] count as overlapping depends on the problem, so ask your interviewer before you write the condition. Second, the extension is Math.max(last[1], cur[1]), not a plain assignment: an interval can be completely swallowed by the previous one ([1,10] then [2,3]), and forgetting the max is the single most common bug interviewers see in this pattern.
Five Classic Problems to Practice
1. Merge Intervals (LeetCode 56)
The canonical form — exactly the template above. Drill it until you can write it from memory in five minutes, including the comparator syntax without hesitation.
2. Insert Interval (LeetCode 57)
A sorted, non-overlapping list plus one new interval. The elegant solution is three phases: add all intervals ending before the new one starts, absorb everything that overlaps into the new interval, then add the rest. No re-sort needed — recognizing that keeps you at O(n) and separates prepared candidates from template reciters.
3. Non-overlapping Intervals (LeetCode 435)
Minimum removals so no intervals overlap. This flips the pattern into a greedy problem: sort by end time and always keep the interval that finishes earliest. Explaining why end-time sorting is optimal — the earliest finisher leaves maximum room for everything after it — is the discussion interviewers actually care about.
4. Meeting Rooms II (LeetCode 253)
Minimum rooms for a meeting schedule — the most-asked interval question at Amazon. Two strong approaches: a min-heap of end times (the heap size is the answer), or the sweep-line trick of sorting starts and ends separately and walking both arrays. Know both; the follow-up “can you do it without a heap?” is practically guaranteed.
5. Employee Free Time (LeetCode 759)
Merge every employee’s schedule, then read out the gaps. It composes the base template with a flattening step, and it is a favorite senior-level screen because it tests whether you see that the gaps between merged regions are the answer.

How to Present It in the Interview
Structure your answer the way strong candidates do. Name the pattern and the sort in your first sentence. State the complexity before coding: O(n log n) time from the sort, O(n) space for the output. Clarify the boundary rule — do touching intervals merge? Then write the template cleanly and walk one example through it, including a swallowed interval, to prove the Math.max line earns its place. Finally, expect follow-ups that pivot the same data into a different variant: “now return the total covered length” or “now find the maximum number of simultaneous intervals.” If you know the five problems above, every one of those pivots is familiar ground.
Your One-Week Practice Plan
Days 1–2: write the merge template from memory and solve LeetCode 56 and 57. Days 3–4: the greedy variants, 435 and 452 (Burst Balloons with arrows). Days 5–6: Meeting Rooms I and II, both the heap and sweep-line solutions. Day 7: Employee Free Time under a 35-minute timer, explaining your reasoning out loud the whole way. Verbalizing under time pressure is a separate skill from solving, and it is the one that decides close calls.
Merge Intervals is the rare pattern where a weekend of focused work converts directly into interview points. The template is ten lines, the edge cases are known in advance, and the variants all rhyme. Rehearsing your delivery matters as much as the code — tools like Niraswa AI can help you practice under realistic conditions. Start now: write the merge template from memory before you close this tab.

