Merge Intervals
Updated
Problem statement
You are given intervals intervals[i] = [start, end] with integer endpoints. Each interval includes both endpoints. Merge every group of overlapping intervals into one interval that covers the same points.
Intervals that share an endpoint overlap. Input intervals may be unsorted, nested, repeated or zero-length. If one interval overlaps two others, merge all three together.
Return the merged intervals, with no overlaps, in increasing order of start. Every original interval must be covered, and no points outside the original union may be introduced. Do not modify the input.
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 become [1,6]. The other two do not touch anything else, so they stay as they are. The answer is [[1,6],[8,10],[15,18]].
Example 2
intervals = [[1,4],[4,5]]return = [[1,5]]The shared endpoint 4 belongs to both closed intervals, so their union is [1,5]. The answer is [[1,5]].
Example 3
intervals = [[4,7],[1,4]]return = [[1,7]]Input order does not matter. Both intervals share endpoint 4 and form [1,7]. The answer is [[1,7]].
Constraints
1 <= intervals.length <= 10000.- Every row contains exactly two integers with
0 <= start <= end <= 10000. - All intervals are closed, including a point interval
[x,x]. - Return integer pairs; endpoint arithmetic fits a signed 32-bit integer.
More Headlands Technologies interview questions
- As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
- Triggered Order Event FilterONSITE INTERVIEW · Seen Oct 2024
- Best Time to Buy and Sell StockPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2024
- Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Apr 2024
- House RobberONSITE INTERVIEW · Seen Apr 2024
- Net Position Calculation from API SnapshotsONSITE INTERVIEW · Seen Apr 2024
- Disaster RecoveryOA · Seen Nov 2026
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Array Interval SumsOA · Seen Oct 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
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 IntervalsSafe Security · 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