Merge Intervals
Updated
Problem statement
Each row [start, end] in intervals includes every point from start through end, including both ends. Merge overlapping intervals until none overlap. The result must cover exactly the same points as the input.
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 Apple Phone Screen questions
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
- Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
- Merge Overlapping IntervalsAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Peak Chat Load Across AgentsCresta · PHONE SCREENCresta interview questions
- Maximum Interactive Team SizeMicrosoft · OAMicrosoft interview questions
- Largest Team Where Every Pair OverlapsWalmart · OAWalmart interview questions
- Meeting Rooms IIeBay · PHONE SCREENeBay interview questions
- Maximum Intervals Overlapping a Fixed-Length WindowBooking.com · ONSITE INTERVIEW
- Meeting Rooms IIByteDance · PHONE SCREENByteDance interview questions
- Majority Labels over Audio IntervalsNooks · PHONE SCREEN
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions