Patterns

LeetCode Interval Problems: 3 Patterns That Solve Every Question

Interval problems are a greedy and sorting sweet spot — learn the 3 core patterns and every interval question becomes straightforward.

10 min read|

Interval problems follow 3 patterns — sort first, then everything clicks

Merge, insert, and schedule: the interval toolkit for coding interviews

Interval Problems Are Predictable Interview Favorites

Leetcode interval problems are among the most common and most predictable questions you will face in a coding interview. Companies love them because they test sorting intuition, greedy reasoning, and careful edge case handling — all in a compact problem that fits neatly into a 45-minute session. If you can recognize the pattern, you can solve the problem.

The beauty of interval questions is that nearly every one falls into one of three categories: merging overlapping intervals, inserting or removing intervals from a set, and selecting the maximum number of non-overlapping intervals. Once you learn these three patterns, the specific problem details become minor variations on a theme you already understand.

This guide breaks down each pattern with real LeetCode problem references, walks through the sorting foundation that underlies all interval work, and highlights the mistakes that trip up even experienced engineers. Whether you are preparing for a FAANG interview or brushing up on fundamentals, mastering the leetcode interval pattern will give you a reliable toolkit for an entire class of problems.

The Sorting Foundation — Why Sorting Comes First in Interval Problems

Before you can merge, insert, or schedule intervals, you need to sort them. This is the single most important insight for leetcode interval problems: sorting by start time (or sometimes end time) transforms a chaotic collection of ranges into a structured sequence you can process in a single linear pass.

Sorting gives you a critical guarantee — once intervals are ordered by their start times, any two intervals that could possibly overlap must be adjacent or nearly adjacent in the sorted order. Without sorting, you would need to compare every interval against every other interval, resulting in O(n^2) comparisons. With sorting, you reduce the problem to O(n log n) for the sort step plus O(n) for the processing pass.

The choice between sorting by start time and sorting by end time depends on the specific pattern. Merge and insert problems typically sort by start time so you can process intervals left to right and detect overlaps as they appear. Scheduling and selection problems often sort by end time because you want to greedily pick the interval that finishes earliest, leaving the most room for future intervals.

  • Sort by start time for merge and insert problems — process intervals left to right
  • Sort by end time for scheduling and selection problems — greedily pick the earliest finish
  • Sorting is O(n log n) and dominates the overall time complexity for most interval solutions
  • After sorting, a single linear pass is usually enough to solve the problem

Merge Overlapping Intervals — The Core Leetcode Interval Pattern

Merge Intervals (LeetCode #56) is the most fundamental interval problem and the one you should master first. The task is simple: given a collection of intervals, merge all overlapping intervals and return the result. It appears constantly in interviews because it tests whether you can sort, detect overlaps, and build a clean output in one pass.

The algorithm starts by sorting intervals by their start time. You initialize a result list with the first interval, then iterate through the remaining intervals. For each interval, you check whether it overlaps with the last interval in your result. Two intervals overlap when the current interval's start time is less than or equal to the previous interval's end time. If they overlap, you merge them by extending the end time of the last result interval to the maximum of the two end times. If they do not overlap, you add the current interval to the result as a new entry.

The time complexity is O(n log n) for sorting plus O(n) for the merge pass, giving O(n log n) overall. Space complexity is O(n) for the output array. Edge cases to watch include intervals that are completely contained within another interval, intervals that share exactly one endpoint (like [1,3] and [3,5] which do overlap because the boundaries are closed), and an input with a single interval.

Getting comfortable with merge intervals leetcode problems builds the intuition you need for every other interval question. The pattern of sort, iterate, and conditionally merge or append is the backbone of interval problem solving.

💡

Pro Tip

Almost every interval problem starts with sorting — if you see intervals, sort by start time first and the solution structure reveals itself.

Insert and Remove Intervals — Handling Non-Overlapping Segments

Insert Interval (LeetCode #57) takes the merge pattern one step further. You are given a set of non-overlapping intervals sorted by start time, plus a new interval to insert. Your job is to insert the new interval, merging any existing intervals that now overlap with it, and return the updated list.

The cleanest approach divides the existing intervals into three groups. First, collect all intervals that end before the new interval starts — these go straight into the result unchanged. Second, process all intervals that overlap with the new interval by continuously expanding the new interval to absorb each overlapping one. Third, append all intervals that start after the merged interval ends. This three-phase approach avoids messy conditional logic and produces clean, readable code.

You can implement this in-place by modifying the original array, or you can build a new result array. The new-array approach is generally cleaner and less error-prone in an interview setting. Either way, the time complexity is O(n) since the input is already sorted — you do not need an additional sort step. This makes insert interval one of the rare interval problems with a linear time solution.

A related challenge is Remove Interval (LeetCode #1272), where instead of inserting you need to subtract an interval from a set of existing intervals. The logic is similar — you split affected intervals at the removal boundaries — but requires careful handling of partial overlaps where an existing interval is only partially removed.

Interval Scheduling and Selection — Greedy End-Time Sorting

The interval scheduling pattern asks a different question: instead of merging intervals, how do you select the maximum number of non-overlapping intervals from a set? This is the classic greedy algorithm problem, and it shows up in several popular LeetCode questions.

Non-overlapping Intervals (LeetCode #435) asks for the minimum number of intervals to remove so that the rest do not overlap. The greedy strategy sorts intervals by end time, then iterates through them keeping track of the current end boundary. Whenever an interval starts before the current end, it overlaps and must be removed. Otherwise, you update the current end to the new interval's end. The answer is the total count minus the number of intervals you kept.

Meeting Rooms (LeetCode #252) is the simplest scheduling problem — just determine whether a person can attend all meetings. Sort by start time, check if any meeting starts before the previous one ends, and return true or false. Meeting Rooms II (LeetCode #253) is the harder follow-up: find the minimum number of conference rooms required. This problem requires tracking overlapping meetings using a min-heap sorted by end time, or alternatively a sweep line approach where you mark start and end events separately.

The key insight for interval scheduling is that sorting by end time and greedily selecting the earliest-finishing interval always produces an optimal solution. This is a provably optimal greedy choice — by finishing as early as possible, you leave the maximum remaining time for future intervals.

  • Non-overlapping Intervals (#435): Sort by end time, greedily keep compatible intervals
  • Meeting Rooms (#252): Sort by start time, check for any overlap — O(n log n)
  • Meeting Rooms II (#253): Use a min-heap or sweep line to track concurrent meetings
  • The greedy end-time strategy is provably optimal for maximum interval selection
⚠️

Watch Out

Meeting Rooms II (#253) requires a min-heap or sweep line, not just greedy — it is the most common follow-up after the basic Meeting Rooms question.

Common Interval Mistakes That Cost You the Interview

The most frequent mistake in leetcode interval problems is forgetting to sort the input. If the problem says intervals are given in arbitrary order, you must sort them before applying any pattern. Skipping the sort step means your linear pass will miss overlaps, producing incorrect results that might pass some test cases but fail on others. Always sort first unless the problem explicitly guarantees sorted input.

Confusing open versus closed boundaries is another common trap. In most LeetCode interval problems, boundaries are closed — meaning [1,3] and [3,5] overlap because they share the point 3. But some problems use open boundaries or mixed boundaries, and the overlap condition changes. Always check the problem statement carefully and adjust your comparison operators accordingly. Using less-than when you need less-than-or-equal (or vice versa) is a subtle bug that is hard to catch under time pressure.

Off-by-one errors with adjacent intervals also cause problems. Two intervals like [1,2] and [3,4] are adjacent but not overlapping under closed boundaries, because 2 is not equal to 3. If the problem asks you to merge adjacent intervals (not just overlapping ones), you need to check whether the gap between them is exactly one. Read the problem constraints twice before deciding on your comparison logic.

Finally, many candidates fail to handle the empty input case or the single-interval case. These are trivial but forgetting them leads to runtime errors. A quick guard clause at the top of your function handles both cases cleanly.

Practice Strategy — Building Interval Problem Intuition with YeetCode

Interval problems reward pattern recognition more than raw problem-solving creativity. Once you have seen the merge, insert, and scheduling patterns, every new interval question is a variation you can map back to a known template. The challenge is not understanding the patterns — it is recalling them quickly under interview pressure.

The most effective way to build that recall is spaced repetition. Instead of solving Merge Intervals once and moving on, you want to revisit it at increasing intervals — after one day, three days, a week, two weeks — until the pattern is automatic. YeetCode flashcards are designed for exactly this approach, giving you bite-sized prompts for each interval pattern so you can drill the core techniques in minutes rather than hours.

Start with Merge Intervals (#56) and Insert Interval (#57) to build your sorting and overlap detection foundation. Then move to Non-overlapping Intervals (#435) and Meeting Rooms (#252, #253) for the greedy scheduling pattern. Once you are comfortable with all five problems, you will find that new interval questions — even ones you have never seen — feel immediately familiar. The pattern does the heavy lifting, and you just adapt the details.

ℹ️

Did You Know

Merge Intervals (#56) and Insert Interval (#57) are two of the top 10 most frequently asked medium problems across all FAANG companies.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now