Insert Interval
Python data engineering interview problem. Difficulty: intermediate. Pattern: Intervals. About 14 minutes. Part of the Pro drill bank.
Insert a new interval into a sorted non-overlapping list and merge. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A new maintenance window must be added to a calendar that is already sorted and non-overlapping. Adding it can join one or two existing windows. Insert new_interval into the existing list, keep the result sorted, and merge every overlap caused by the insertion. Do not lose intervals that are completely before or after the new one. Write the function insert_interval(intervals: list, new_interval: list) -> list. In [[1, 3], [6, 9]], inserting [2, 5] joins the first interval and produces [[1, 5], [6, 9]]. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.
Requirements
- Keep intervals before the new interval unchanged.
- Merge the new interval with every overlapping interval.
- Append later intervals in sorted order.
Constraints
- The original intervals are sorted and do not overlap.
- The return type is a list of [start, end] pairs, and endpoints follow the visible sample contract.
Examples
Input: insert_interval([[1, 3], [6, 9]], [2, 5]) Output: [[1, 5], [6, 9]] New interval merges with [1,3].
Topics: lakebench, python, insert, merge.
More Python interview questions · All interview problems · Learn data engineering
Insert Interval
Interview-style drill: Insert a new interval into a sorted non-overlapping list and merge.
Implement `insert_interval(intervals: list, new_interval: list) -> list`. Example: `[[1, 3], [6, 9]], [2, 5]` → `[[1, 5], [6, 9]]`. Keep the harness.