Quick Overview

This question evaluates array and interval manipulation skills, algorithmic problem-solving, and the ability to reason about merging overlapping ranges in a sorted sequence.

Insert and merge an interval

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a list of pairwise non-overlapping closed intervals [si, ei] sorted by start time, and a new interval [s, e]. Insert [s, e] into the list so that the result remains sorted and contains no overlaps (merge when necessary). Return the resulting list of intervals. Analyze time and space complexity and cover edge cases such as insertion at the beginning/end, full containment, and an empty input list.

Overview: This question evaluates array and interval manipulation skills, algorithmic problem-solving, and the ability to reason about merging overlapping ranges in a sorted sequence.

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

Insert a new interval into sorted non-overlapping closed intervals and merge overlaps.

Constraints

  • Inputs are provided as Python literals matching the function signature.
  • Return a deterministic exact-match result.

Examples

Input: ([[1,3],[6,9]], [2,5])

Expected Output: [[1, 5], [6, 9]]

Explanation: Merge with first.

Input: ([[1,2],[3,5],[6,7],[8,10],[12,16]], [4,8])

Expected Output: [[1, 2], [3, 10], [12, 16]]

Explanation: Merge several.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.

Insert and Merge an Interval

mediumAmazonSoftware Engineer

Insert a new interval into sorted non-overlapping closed intervals and merge overlaps.

Examples

Example 1
Input
([[1,3],[6,9]], [2,5])
Output
[[1, 5], [6, 9]]
Notes
Merge with first.
Example 2
Input
([[1,2],[3,5],[6,7],[8,10],[12,16]], [4,8])
Output
[[1, 2], [3, 10], [12, 16]]
Notes
Merge several.

Constraints

  • •Inputs are provided as Python literals matching the function signature.
  • •Return a deterministic exact-match result.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Sample call
solution([[1,3],[6,9]], [2,5])
Language
Case 1
Input
([[1,3],[6,9]], [2,5])
Expected
[[1, 5], [6, 9]]
Case 2
Input
([[1,2],[3,5],[6,7],[8,10],[12,16]], [4,8])
Expected
[[1, 2], [3, 10], [12, 16]]

Browse More Questions