Search⌘ K
AI Features

Merge Intervals

Understand how to merge overlapping closed intervals and produce non-overlapping results. Explore problem constraints and develop optimized interval logic to solve common interview tasks confidently.

Statement

We are given an array of closed intervalsclosedintervals called intervals, where each interval has a start time and an end time and is represented as intervals[i] = [starti, endi]. Your task is to merge all the overlapping intervalsOverlapping intervals are two or more intervals with at least one common point in time. and return an array of the resulting non-overlapping intervals that cover all the intervals in the input.

Constraints:

  • 11 \leq intervals.length 103\leq10^3

  • intervals[i].length ==2== 2

  • 00\leq starti \leq endi 104\leq10^4

Examples

canvasAnimation-image
1 / 4

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Merge Intervals

1.

Given the intervals below, what is the correct output after merging the overlapping intervals?

[ [1, 6], [2, 4] ]

A.

[ [2, 4] ]

B.

[ [1, 6] ]

C.

[ [1, 6], [2, 4] ]

D.

[ [2, 4], [1, 6] ]


1 / 4

Figure it out

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

Go
usercode > main.go
package main
func mergeIntervals(intervals [][]int) [][]int {
// Replace this placeholder return statement with your code
return make([][]int, 0)
}
Merge Intervals