Merge Intervals
Updated
Problem statement
Given an array of closed intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals.
Intervals that share an endpoint overlap. Return the non-overlapping intervals that cover every input interval, sorted by increasing start value.
Function
merge(intervals: int[][]) → int[][]Examples
Example 1
intervals = [[1,3],[2,6],[8,10],[15,18]]return = [[1,6],[8,10],[15,18]]Intervals [1,3] and [2,6] overlap and merge into [1,6]. The other intervals remain separate.
Example 2
intervals = [[1,4],[4,5]]return = [[1,5]]The intervals share endpoint 4, so the closed intervals overlap.
Example 3
intervals = [[8,10],[1,4],[2,3],[15,18],[6,9],[3,7],[17,20],[12,12]]return = [[1,10],[12,12],[15,20]]Sorting exposes two overlap chains: the intervals from [1,4] through [8,10] merge into [1,10], and the last two merge into [15,20].
Constraints
1 <= intervals.length <= 10000intervals[i].length == 2-10^9 <= start_i <= end_i <= 10^9
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Cars for Rental RequestsGoogle · OA · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Merge Overlapping IntervalsAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Concurrent Processes (Bar Raiser Round)Amazon · PHONE SCREEN · ONSITE INTERVIEW
- Meeting Scheduling SystemHevo Data · ONSITE INTERVIEW
- Maximum Intervals Overlapping a Fixed-Length WindowBooking.com · ONSITE INTERVIEW
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Minimum Meeting Rooms with AssignmentsUber · PHONE SCREEN · ONSITE INTERVIEWUber interview questions
- Peak Bike Demand IntervalsFaire · ONSITE INTERVIEW