Merge Overlapping Intervals, Then Insert a New Interval Using Binary Search

Quick Overview

A two-part coding exercise on intervals: merge possibly overlapping integer intervals into a sorted, disjoint union, then insert a new interval into that merged list, using binary search to find the affected stretch. It tests sorting, endpoint conventions, boundary conditions and self-written tests.

Merge Overlapping Intervals, Then Insert a New Interval Using Binary Search

Company: Apple

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

You are given a list of intervals, each a pair `[start, end]` of integers with `start <= end`. The coding round was done in a shared editor where the candidate had to write and run their own tests. The report says the problem was an adaptation of the well-known task of merging overlapping intervals, and that one of the follow-ups should have been solved with binary search, which the candidate did not see at the time. The exact adaptation and follow-up were not reported, so the two parts below are a reconstruction to confirm with the interviewer: the base merge, then a follow-up on the merged result where binary search applies. ### Clarifying Questions - Should intervals that only share an endpoint, such as `[1, 3]` and `[3, 5]`, be merged? - Is the input sorted, and may the function reorder or modify it? - Can the list be empty, and can an interval be a single point, such as `[4, 4]`? - How many intervals are there, and how many follow-up operations will run against the merged result? ### Part 1 — Merge overlapping intervals Return the union of the intervals as a list of disjoint intervals sorted by start, merging every pair that overlaps. Unless the interviewer says otherwise, intervals that share an endpoint also merge. ```python def merge(intervals: list[list[int]]) -> list[list[int]]: ``` For example, `merge([[8, 10], [1, 3], [2, 6], [15, 18], [6, 7]])` returns `[[1, 7], [8, 10], [15, 18]]`: `[1, 3]` and `[2, 6]` overlap, and `[6, 7]` shares the endpoint 6 with their union. ```hint Choose an order Find an order of the intervals in which you can decide whether the next interval joins a group by comparing it only with the last group you built. ``` #### What This Part Should Cover - An ordering that makes a single merging pass possible, and why that pass is correct - Containment, shared endpoints, single-point intervals, and unsorted or empty input - Time and space cost, and tests the candidate writes and runs for these cases ### Part 2 — Insert a new interval into the merged list Now the intervals are already merged, sorted by start and pairwise disjoint, exactly as Part 1 returns them. Given one new interval, return the merged list after adding it. Use the fact that the list is sorted instead of running Part 1 again. ```python def insert(merged: list[list[int]], new: list[int]) -> list[list[int]]: ``` For example, `insert([[1, 3], [5, 6], [8, 9], [11, 14], [20, 22]], [6, 11])` returns `[[1, 3], [5, 14], [20, 22]]`: the new interval shares an endpoint with `[5, 6]`, covers `[8, 9]` and shares an endpoint with `[11, 14]`, so all four combine. ```hint Where the change happens The new interval can affect only one contiguous stretch of the list. Think about how to find where that stretch begins and ends without walking through the intervals before it. ``` #### Clarifying Questions for this Part - Should the function return a new list or update `merged` in place? - Will many intervals be inserted one after another, or many point lookups made, against the same list? #### What This Part Should Cover - Locating the affected stretch by searching the sorted list, with the exact condition used at each end - Combining the affected intervals with the new one, including when it overlaps nothing, overlaps everything, or falls before or after all intervals - Which steps binary search speeds up, and which still take linear time in a Python list ### What a Strong Answer Covers - Correct, tested code for both parts - One endpoint convention, stated and applied the same way in both parts - The complexity of each part, with an accurate account of what binary search saves - Knowing when a sorted container beats re-merging after every change ### Follow-up Questions - Given the merged list, how would you answer many queries of the form "is point x covered, and by which interval?" - If insertions and point queries arrive continuously, which data structure keeps both fast? - How would you keep the total covered length up to date as intervals are inserted?

Overview: A two-part coding exercise on intervals: merge possibly overlapping integer intervals into a sorted, disjoint union, then insert a new interval into that merged list, using binary search to find the affected stretch. It tests sorting, endpoint conventions, boundary conditions and self-written tests.

Read the full Apple Software Engineer interview experience this question came from

Apple logo
Apple
Jun 25, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

You are given a list of intervals, each a pair [start, end] of integers with start <= end. The coding round was done in a shared editor where the candidate had to write and run their own tests. The report says the problem was an adaptation of the well-known task of merging overlapping intervals, and that one of the follow-ups should have been solved with binary search, which the candidate did not see at the time. The exact adaptation and follow-up were not reported, so the two parts below are a reconstruction to confirm with the interviewer: the base merge, then a follow-up on the merged result where binary search applies.

Clarifying Questions Guidance

  • Should intervals that only share an endpoint, such as [1, 3] and [3, 5] , be merged?
  • Is the input sorted, and may the function reorder or modify it?
  • Can the list be empty, and can an interval be a single point, such as [4, 4] ?
  • How many intervals are there, and how many follow-up operations will run against the merged result?

Part 1 — Merge overlapping intervals

Return the union of the intervals as a list of disjoint intervals sorted by start, merging every pair that overlaps. Unless the interviewer says otherwise, intervals that share an endpoint also merge.

def merge(intervals: list[list[int]]) -> list[list[int]]:

For example, merge([[8, 10], [1, 3], [2, 6], [15, 18], [6, 7]]) returns [[1, 7], [8, 10], [15, 18]]: [1, 3] and [2, 6] overlap, and [6, 7] shares the endpoint 6 with their union.

What This Part Should Cover Guidance

  • An ordering that makes a single merging pass possible, and why that pass is correct
  • Containment, shared endpoints, single-point intervals, and unsorted or empty input
  • Time and space cost, and tests the candidate writes and runs for these cases

Part 2 — Insert a new interval into the merged list

Now the intervals are already merged, sorted by start and pairwise disjoint, exactly as Part 1 returns them. Given one new interval, return the merged list after adding it. Use the fact that the list is sorted instead of running Part 1 again.

def insert(merged: list[list[int]], new: list[int]) -> list[list[int]]:

For example, insert([[1, 3], [5, 6], [8, 9], [11, 14], [20, 22]], [6, 11]) returns [[1, 3], [5, 14], [20, 22]]: the new interval shares an endpoint with [5, 6], covers [8, 9] and shares an endpoint with [11, 14], so all four combine.

Clarifying Questions for this Part Guidance

  • Should the function return a new list or update merged in place?
  • Will many intervals be inserted one after another, or many point lookups made, against the same list?

What This Part Should Cover Guidance

  • Locating the affected stretch by searching the sorted list, with the exact condition used at each end
  • Combining the affected intervals with the new one, including when it overlaps nothing, overlaps everything, or falls before or after all intervals
  • Which steps binary search speeds up, and which still take linear time in a Python list

What a Strong Answer Covers Guidance

  • Correct, tested code for both parts
  • One endpoint convention, stated and applied the same way in both parts
  • The complexity of each part, with an accurate account of what binary search saves
  • Knowing when a sorted container beats re-merging after every change

Follow-up Questions Guidance

  • Given the merged list, how would you answer many queries of the form "is point x covered, and by which interval?"
  • If insertions and point queries arrive continuously, which data structure keeps both fast?
  • How would you keep the total covered length up to date as intervals are inserted?