Merge Intervals

Python data engineering interview problem. Difficulty: intermediate. Pattern: Intervals. About 14 minutes. Part of the Pro drill bank.

Merge all overlapping intervals. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Maintenance jobs are described by start and end times. 2 jobs that overlap can be represented by one larger interval so downstream scheduling does not process the same time range twice. Merge every pair of overlapping intervals and return a sorted list of disjoint intervals. Intervals that touch at an endpoint are considered separate only when the sample rule keeps them separate; use the problem's visible output as the contract. Write the function merge_intervals(intervals: list) -> list. [1, 3] overlaps [2, 6], so they become [1, 6]; [8, 10] and [15, 18] stay separate. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.

Requirements

  • Sort intervals by their start before merging.
  • Extend the current interval when the next one overlaps.
  • Return a new list ordered by start time.

Constraints

  • Each interval is [start, end] and the input may be unsorted.
  • The return type is a list of 2-item lists, with no overlapping intervals left.

Examples

Input: merge_intervals([[1, 3], [2, 6], [8, 10], [15, 18]]) Output: [[1, 6], [8, 10], [15, 18]] 1-3 and 2-6 merge.

Topics: lakebench, python, merge, intervals.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Merge Intervals

Interview-style drill: Merge all overlapping intervals.

Implement `merge_intervals(intervals: list) -> list` where each interval is `[start, end]`. Example: `[[1, 3], [2, 6], [8, 10], [15, 18]]` → `[[1, 6], [8, 10], [15, 18]]`. Keep the harness.