Merge Intervals
Updated
Problem statement
Each row [start, end] in intervals is a closed interval. Merge overlapping intervals until the result contains disjoint intervals covering exactly the same points.
Intervals that share an endpoint overlap. Return the merged intervals in ascending order of their starts.
Function
mergeIntervals(intervals: int[][]) → int[][]Examples
Example 1
intervals = [[1,3],[2,6],[8,10],[15,18]]return = [[1,6],[8,10],[15,18]]The first two intervals overlap and merge to [1,6].
Example 2
intervals = [[1,4],[4,5]]return = [[1,5]]The shared endpoint 4 makes these closed intervals overlap.
Example 3
intervals = [[4,7],[1,4]]return = [[1,7]]Sort the starts, then merge at the shared endpoint.
Example 4
intervals = [[1,4],[3,5],[6,8],[10,12],[8,9]]return = [[1,5],[6,9],[10,12]]The first two intervals merge to [1,5]. Intervals [6,8] and [8,9] share endpoint 8 and merge to [6,9]. Interval [10,12] remains separate.
Constraints
1 <= intervals.length <= 10^4.- Each row has two entries with
0 <= start <= end <= 10^4.
More Safe Security Onsite Interview questions
Similar questions at other companies
- Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Merge Overlapping IntervalsAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- 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
- Merge IntervalsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Merge Overlapping IntervalsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Meeting Rooms IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Removals for Non-Overlapping IntervalsByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge IntervalsBloomberg LP · OA · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions