Merge Intervals
Interval Management / SortingO(n log n)Solved Nov 9, 2025
Asked at 12 companies
12 recent
- Tesla
- Ozon
- IBM
- Walmart Labs
- Bloomberg
- eBay
- Meta
- Apple
- Atlassian
- Visa
+2 moreShow fewer
- TikTok
- Yandex
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: When sorting first unlocks the pattern · Sorting costs O(n log n) · What a heap keeps on top
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Calendars and ZIP files: Booking meeting rooms is interval scheduling, and ZIP compression builds its Huffman codes greedily, merging the two rarest symbols each step.
ProblemProblem statementGiven an array of intervals, where intervals[i] = [starti, endi], merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.ReadHide
Problem Description
Given an array of intervals, where intervals[i] = [start_i, end_i], merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Sort the intervals by their start time. Iterate through the sorted list, merging the current interval with the last merged interval if they overlap.
Approaches
1. Sorting and Greedy Merge (Optimal) - O(N log N) ⭐
Idea: This is the core algorithm for interval management. Sorting ensures that as we iterate, any potential overlaps must occur only with the last merged interval.
- Sort the intervals based on their start time (
O(N log N)). - Initialize the result list with the first interval.
- Iterate through the rest of the sorted intervals:
- If the current interval overlaps with the last merged interval (i.e., its start
≤last merged end), merge by updating the last merged end tomax(last end, current end). - If there is no overlap, add the current interval to the result list.
- If the current interval overlaps with the last merged interval (i.e., its start
2. Brute Force - O(N²)
Idea: Compare every interval against every other interval to see if they overlap, and merge them repeatedly until no more merges are possible. This results in slow performance due to redundant checks.
Solution code
import sys
from typing import List
class Solution:
# Optimized: O(N log N) Time, O(N) Space - Sorting and Greedy Merge
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []
# 1. Sort the intervals by the start time (O(N log N))
intervals.sort(key=lambda x: x[0])
merged = []
current_start, current_end = intervals[0]
for i in range(1, len(intervals)):
next_start, next_end = intervals[i]
# Check for overlap: current end >= next start
if current_end >= next_start:
# Merge: Update the end time to the maximum of the two ends
current_end = max(current_end, next_end)
else:
# No overlap: Add the merged interval and start a new one
merged.append([current_start, current_end])
current_start, current_end = next_start, next_end
# Add the last merged interval
merged.append([current_start, current_end])
return merged
# The Brute Force O(N^2) solution is highly discouraged and complex to implement correctly
# due to repeated merging passes.Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language