FastPrepMerge Intervals
Problems/Merge Intervals

Merge Intervals

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Headlands Technologies onsite interview. The report names Merge Intervals. We rebuilt the exact rules for joining closed intervals and set the limits. We chose the function name and listed the output in increasing order of start. The report did not include the original interview code. Our version is about 90% the same as the reported question.

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
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public int[][] mergeIntervals(int[][] intervals) {
        // write your code here
    }
}
Checking account…