PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates competence in interval and array manipulation, sorting-based algorithm design, overlap detection, and efficient time/space complexity reasoning.

  • medium
  • Apple
  • Coding & Algorithms
  • Software Engineer

Merge overlapping time intervals

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem: Merge Intervals You are given an array of intervals `intervals`, where each interval is a pair `[start, end]` with `start <= end`. Merge all overlapping intervals and return a new array of non-overlapping intervals that covers all the intervals in the input. ### Overlap definition Two intervals `[a,b]` and `[c,d]` overlap if `c <= b` (assuming `a <= c` after sorting). ### Input / Output - **Input:** `intervals: number[][]` - **Output:** `number[][]` merged intervals ### Example - Input: `[[1,3],[2,6],[8,10],[15,18]]` - Output: `[[1,6],[8,10],[15,18]]` ### Constraints - `1 <= intervals.length <= 10^5` - Interval values fit in 32-bit signed integers. ### Follow-ups - What is the time complexity of your approach? - How do you handle already-sorted vs unsorted inputs?

Quick Answer: This question evaluates competence in interval and array manipulation, sorting-based algorithm design, overlap detection, and efficient time/space complexity reasoning.

You are given a list of time intervals, where each interval is represented as [start, end] and start <= end. Merge all overlapping intervals and return a new list of non-overlapping intervals that covers exactly the same ranges as the input. After sorting by start time, two intervals [a, b] and [c, d] overlap if c <= b. Intervals that touch at an endpoint, such as [1, 3] and [3, 5], should be merged into [1, 5].

Constraints

  • 1 <= intervals.length <= 100000
  • Each interval has exactly two integers: [start, end]
  • start <= end for every interval
  • Interval values fit in 32-bit signed integers

Examples

Input: ([[1,3],[2,6],[8,10],[15,18]],)

Expected Output: [[1,6],[8,10],[15,18]]

Explanation: [1,3] and [2,6] overlap, so they merge into [1,6]. The other intervals do not overlap.

Input: ([[5,7],[1,4],[4,5],[10,12]],)

Expected Output: [[1,7],[10,12]]

Explanation: After sorting, [1,4], [4,5], and [5,7] all overlap or touch at endpoints, so they merge into [1,7].

Input: ([[42,42]],)

Expected Output: [[42,42]]

Explanation: A single interval is already merged.

Input: ([[-10,-1],[-5,0],[1,3],[3,3],[2,6],[-10,-1]],)

Expected Output: [[-10,0],[1,6]]

Explanation: The negative intervals, including the duplicate [-10,-1], merge into [-10,0]. The intervals [1,3], [3,3], and [2,6] merge into [1,6].

Input: ([[1,10],[2,3],[4,8],[11,12]],)

Expected Output: [[1,10],[11,12]]

Explanation: [2,3] and [4,8] are fully contained inside [1,10], so the merged interval remains [1,10].

Input: ([[-2147483648,-2147483648],[-2147483648,0],[2147483646,2147483647]],)

Expected Output: [[-2147483648,0],[2147483646,2147483647]]

Explanation: The first two intervals overlap at the minimum 32-bit integer boundary and merge into [-2147483648,0].

Hints

  1. If the intervals are not already sorted, sorting by start time makes it easier to identify overlaps in one pass.
  2. Keep track of the most recently merged interval and compare each next interval's start with the merged interval's end.
Last updated: Jun 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)