Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Intervals High frequency Solution Available Practice Mode

Merge Intervals LeetCode Solution

Tags: #Array #Sorting

Approach Summary

Sort by start. Merge current with last result if overlapping. Two intervals overlap when start2 <= end1.

Practice Mode 3 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Intervals Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Intervals pattern.

Framework: Intervals • Interview Prep: Tested at FAANG • Explore all Intervals problems & invariants →

How to Recognize This Pattern

  • Merge overlapping intervals
  • Sort by start

Complexity Analysis

Time Complexity

O(n log n)

Space Complexity

O(n)

Tags

Array Sorting

Frequently Asked Questions

What is the optimal time and space complexity for Merge Intervals?
The optimal solution for Merge Intervals achieves O(n log n) runtime complexity and requires O(n) auxiliary space. Time complexity is O(n log n) dominated by the initial sorting step. The linear scan takes O(n). Space complexity is O(n) or O(log n) depending on the sorting implementation.
How do I identify that Merge Intervals requires the Intervals pattern?
In technical interviews, look for key problem signals: Merge overlapping intervals; Sort by start. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Intervals strategy.
What critical edge cases must be handled in Merge Intervals?
Essential edge cases to verify include: Single interval (e.g. [[1, 4]]; returns [[1, 4]]); All intervals overlap into one (e.g. [[1, 4], [2, 5], [3, 6]]; returns [[1, 6]]); No overlapping intervals (e.g. [[1, 2], [3, 4], [5, 6]]; returns identical list); Touching intervals (e.g. [1, 4] and [4, 5] touch at 4; merged into [1, 5]). Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Merge Intervals?
Merge Intervals is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Facebook, Amazon, Google, Twitter, Stripe.
Buy me a coffee
Daily Practice
0/0