Quick Overview

Merge overlapping closed intervals with a sorted scan, preserving nested coverage, handling touching endpoints, and explaining the union invariant and complexity.

Merge Overlapping Intervals

Company: Atlassian

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given intervals `[start,end]`, merge all overlapping intervals and return the resulting nonoverlapping intervals in ascending start order. ### Constraints & Assumptions Use closed intervals with start at most end, so intervals touching at an endpoint overlap. This is the standard formulation of the named merge-intervals problem; clarify endpoint semantics for half-open intervals. ### Clarifying Questions to Ask - Should `[1,3]` and `[3,5]` merge? - May the input be sorted or mutated in place? ```hint Sort away the uncertainty Once intervals are ordered by start, only the final merged interval can overlap the next one. ``` ### What a Strong Answer Covers - Sorting by start and extending the current merged end. - Nested intervals, equal endpoints, and disjoint gaps. - Correct empty input and complexity. ### Follow-up Questions - What changes for half-open intervals when touching should remain separate? - How would you insert one interval into an already merged sorted list?

Overview: Merge overlapping closed intervals with a sorted scan, preserving nested coverage, handling touching endpoints, and explaining the union invariant and complexity.

Given a list of closed intervals `[start, end]`, merge all overlapping intervals. Return the resulting nonoverlapping intervals in ascending order of start. A shared endpoint counts as overlap.

Constraints

  • The list contains 0 through 2^31 - 1 intervals.
  • Every interval is exactly [start, end] with start <= end.
  • Both endpoints are integers from -(2^53 - 1) to 2^53 - 1, inclusive.

Examples

Input: ([[9, 12], [2, 6], [1, 4], [15, 17]],)

Expected Output: [[1, 6], [9, 12], [15, 17]]

Explanation: The first two intervals after ordering by start overlap. Two later gaps remain.

Input: ([[4, 8], [1, 4], [8, 8]],)

Expected Output: [[1, 8]]

Explanation: The intervals meet at endpoints, so their union is a single closed interval.

Hints

  1. When checking whether two closed intervals overlap, include both endpoints.

Merge Overlapping Intervals

mediumAtlassianMachine Learning Engineer

Given a list of closed intervals [start, end], merge all overlapping intervals. Return the resulting nonoverlapping intervals in ascending order of start. A shared endpoint counts as overlap.

View full question (all parts)

Given intervals [start,end], merge all overlapping intervals and return the resulting nonoverlapping intervals in ascending start order.

Constraints & Assumptions

Use closed intervals with start at most end, so intervals touching at an endpoint overlap. This is the standard formulation of the named merge-intervals problem; clarify endpoint semantics for half-open intervals.

Clarifying Questions to Ask

  • Should [1,3] and [3,5] merge?
  • May the input be sorted or mutated in place?

What a Strong Answer Covers

  • Sorting by start and extending the current merged end.
  • Nested intervals, equal endpoints, and disjoint gaps.
  • Correct empty input and complexity.

Follow-up Questions

  • What changes for half-open intervals when touching should remain separate?
  • How would you insert one interval into an already merged sorted list?

Examples

Example 1
Input
([[9, 12], [2, 6], [1, 4], [15, 17]],)
Output
[[1, 6], [9, 12], [15, 17]]
Notes
The first two intervals after ordering by start overlap. Two later gaps remain.
Example 2
Input
([[4, 8], [1, 4], [8, 8]],)
Output
[[1, 8]]
Notes
The intervals meet at endpoints, so their union is a single closed interval.

Constraints

  • •The list contains 0 through 2^31 - 1 intervals.
  • •Every interval is exactly [start, end] with start <= end.
  • •Both endpoints are integers from -(2^53 - 1) to 2^53 - 1, inclusive.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
Sample call
merge_intervals([[9, 12], [2, 6], [1, 4], [15, 17]])
Language
Case 1
Input
([[9, 12], [2, 6], [1, 4], [15, 17]],)
Expected
[[1, 6], [9, 12], [15, 17]]
Case 2
Input
([[4, 8], [1, 4], [8, 8]],)
Expected
[[1, 8]]

Browse More Questions

Show the approach

Approach

Sort the intervals by start and scan them in that order. When an interval starts after the last merged end, append it as a new interval. Otherwise, extend the last end to the greater of its current end and the incoming end. The equality case merges because the intervals are closed.

After each step, the result is a sorted, disjoint representation of the union of the processed intervals. Every earlier result ends before the start of the final result, so a new start cannot overlap any of those earlier intervals. It either overlaps the final result and extends that union, or starts a new component. This preserves the invariant through the end of the scan.

Time complexity:
O(n log n) for n intervals.
Space complexity:
O(n) auxiliary space for the sorted copy, plus O(n) for the returned intervals.