Dataford
Interview Guides
Upgrade
All questions/Coding/Merge Overlapping Time Intervals

Merge Overlapping Time Intervals

Medium
Coding
Asked at 14 companies14ArraysSortingGreedy
Also asked at
AMetaCox AutomotiveDidi ChuxingOtter.aiFoursquare

Problem

Problem

In a Meta scheduling or logging pipeline, you may receive a list of time ranges that can overlap. Write a function that merges all overlapping intervals and returns a minimal list of non-overlapping intervals.

Formal Specification

Given intervals, a list of intervals where each interval is a two-element list [start, end], return a new list of merged intervals sorted by start time.

Two intervals overlap if the next interval's start is less than or equal to the current merged interval's end. In that case, they should be combined into [min(start1, start2), max(end1, end2)].

Examples

Example 1 Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]]

Explanation: [1,3] and [2,6] overlap, so they merge into [1,6].

Example 2 Input: intervals = [[1,4],[4,5]] Output: [[1,5]]

Explanation: The intervals touch at 4, so they are considered overlapping and should be merged.

Constraints

  • 0 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • 0 <= start <= end <= 10^9
  • The input intervals may be unsorted

Examples

Example 1
Inputintervals = [[1,3],[2,6],[8,10],[15,18]]Output[[1,6],[8,10],[15,18]]WhyThe first two intervals overlap and merge into `[1,6]`. The remaining intervals do not overlap with that merged range.
Example 2
Inputintervals = [[1,4],[4,5]]Output[[1,5]]WhyBecause touching endpoints are treated as overlapping, `[1,4]` and `[4,5]` merge into one interval.
Example 3
Inputintervals = [[5,7],[1,2],[2,4]]Output[[1,4],[5,7]]WhyAfter sorting, `[1,2]` and `[2,4]` merge into `[1,4]`, while `[5,7]` remains separate.

Constraints

  • 0 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • 0 <= start <= end <= 10^9
  • The input intervals may be unsorted

Function Signature

def merge_intervals(intervals):

Problem

Problem

In a Meta scheduling or logging pipeline, you may receive a list of time ranges that can overlap. Write a function that merges all overlapping intervals and returns a minimal list of non-overlapping intervals.

Formal Specification

Given intervals, a list of intervals where each interval is a two-element list [start, end], return a new list of merged intervals sorted by start time.

Two intervals overlap if the next interval's start is less than or equal to the current merged interval's end. In that case, they should be combined into [min(start1, start2), max(end1, end2)].

Examples

Example 1 Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]]

Explanation: [1,3] and [2,6] overlap, so they merge into [1,6].

Example 2 Input: intervals = [[1,4],[4,5]] Output: [[1,5]]

Explanation: The intervals touch at 4, so they are considered overlapping and should be merged.

Constraints

  • 0 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • 0 <= start <= end <= 10^9
  • The input intervals may be unsorted

Examples

Example 1
Inputintervals = [[1,3],[2,6],[8,10],[15,18]]Output[[1,6],[8,10],[15,18]]WhyThe first two intervals overlap and merge into `[1,6]`. The remaining intervals do not overlap with that merged range.
Example 2
Inputintervals = [[1,4],[4,5]]Output[[1,5]]WhyBecause touching endpoints are treated as overlapping, `[1,4]` and `[4,5]` merge into one interval.
Example 3
Inputintervals = [[5,7],[1,2],[2,4]]Output[[1,4],[5,7]]WhyAfter sorting, `[1,2]` and `[2,4]` merge into `[1,4]`, while `[5,7]` remains separate.

Constraints

  • 0 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • 0 <= start <= end <= 10^9
  • The input intervals may be unsorted

Function Signature

def merge_intervals(intervals):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
MetaMerge Overlapping Time RangesMediumWalmartMerge Overlapping Time IntervalsMediumTwitchMerge Overlapping IntervalsMedium
Next question