Prepin
Log in

Combine Intervals

Approach / Explanation

The "Merge Intervals" problem is a common algorithmic challenge encountered during job interviews. This task involves merging all overlapping intervals and returning an array of intervals that comprehensively cover the input set. Typically, the problem is stated as follows: Given a collection of intervals, merge all overlapping intervals. To grasp this problem, you should visualize an array of intervals . Each interval i consists of an array of two elements, where interval[i][0] denotes the start of the interval and interval[i][1] indicates the end of the interval. Your goal is to merge all overlapping intervals and return an array of non-overlapping intervals that encompass all the intervals provided in the input. The essential steps to solve the problem are: Sort the intervals based on their start times. Iterate through the sorted intervals, and for each interval, compare its start time with the end time of the last interval in the result. If they overlap, merge them by updating the end time of the last interval. If they do not overlap, simply add the current interval to the result.

Suggested Answer

Let’s illustrate this with an example: Consider a set of intervals: [[1,3],[2,6],[8,10],[15,18]]. Here are the steps to address this problem: First, sort the intervals, resulting in: [[1,3],[2,6],[8,10],[15,18]]. Start the resulting list with the first interval, which gives: [[1,3]]. Next, iterate through the intervals from the second one to the last, that is, from [2,6] to [15,18]. For each current interval: If the start of the current interval is greater than the end of the last interval in the result list, add the current interval to the result list. If the start of the current interval is less than the end of the last interval, merge the current interval with the last interval in the result list by updating the end of the last interval to be the maximum of the two ends. After completing these steps, the final output will be [[1,6],[8,10],[15,18]].

Alternative Answer

Let’s examine another instance with intervals represented as [[4,5],[1,4]]. First, sort the intervals, resulting in: [[1,4],[4,5]] Start the resulting list with the first interval, which gives: [[1,4]] Iterate through the intervals from the second one to the last, in this case [4,5]. For each current interval: If the start of the current interval is greater than the end of the last interval in the result list, add the current interval to the result list. If the start of the current interval is less than or equal to the end of the last interval, merge the current interval with the last interval in the result list by updating the end of the last interval to be the maximum of the end of the last interval and the end of the current interval. After completing these steps, the final output will be [[1,5]]. This is the method for merging intervals. Keep in mind that sorting the intervals at the beginning is essential for this approach.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.