📖 4 min read
Last updated on

Insert Interval — Coding Interview Walkthrough


Hero Image for Insert Interval — Coding Interview Walkthrough

You’re in a coding interview. The interviewer says: “You have a list of non-overlapping intervals sorted by start time. Insert a new interval and merge any overlaps.” You know this is a linear scan — no sort needed because the input is already sorted. The challenge is the boundary conditions: when does the new interval overlap, when does the current interval go before it, and when does it go after?


Already know how to solve Insert Interval? Try a Insert Interval mock interview on Intervu →

The Problem

You are given an array of non-overlapping intervals intervals sorted in ascending order by start time, and a new interval newInterval. Insert newInterval into intervals, merging any overlapping intervals.

Example

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16]]

Interval timeline with before row showing [1,2],[3,5],[6,7],[8,10],[12,16] plus pink insert→ bar [4,8], and after row showing merged result [1,2],[3,10],[12,16].

What Interviewers Are Actually Testing

SkillWhat Interviewers Observe
Case analysisDo you identify the three cases cleanly?
Boundary conditionsDo you use <= vs. < correctly for overlap?
No re-sortDo you recognize no sort step is needed?
Edge casesEmpty intervals, newInterval before/after all?

Step 1: The Three Cases

For each existing interval relative to newInterval:

  1. Ends before (curr[1] < new_start): no overlap, add as-is.
  2. Overlaps (curr[0] <= new_end): merge by expanding bounds.
  3. Starts after (curr[0] > new_end): insert merged newInterval, then add rest.

Step 2: The Key Insight

Treat the insertion as a running interval that expands as it absorbs overlapping neighbors. Scan left to right — anything before gets added directly, anything overlapping merges, everything after the overlap triggers the insertion.


Python Implementation

def insert(intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
    result = []
    i = 0
    n = len(intervals)

    # Add intervals that end before newInterval starts
    while i < n and intervals[i][1] < newInterval[0]:
        result.append(intervals[i])
        i += 1

    # Merge overlapping intervals
    while i < n and intervals[i][0] <= newInterval[1]:
        newInterval[0] = min(intervals[i][0], newInterval[0])
        newInterval[1] = max(intervals[i][1], newInterval[1])
        i += 1

    result.append(newInterval)

    # Add remaining intervals
    result.extend(intervals[i:])
    return result

Time & Space Complexity

AspectComplexity
TimeO(n) — single scan
SpaceO(n) — output list

Common Interview Mistakes

  1. Wrong overlap condition. Use <= not < — adjacent intervals like [1,3] and [3,5] should merge.
  2. Forgetting to append merged interval. Many candidates exit the merge loop and skip appending newInterval.
  3. Unnecessary sort. Input is already sorted — sorting costs O(n log n) for no reason.

What a Strong Candidate Sounds Like

“The input is already sorted, so I scan left to right in O(n). Three cases: intervals ending before the new one get added as-is, overlapping ones merge by expanding bounds, and once past the overlap I insert the merged interval and append the rest.”


Example Interview Dialogue

Interviewer: What’s the overlap condition?

Candidate: Two intervals [a,b] and [c,d] overlap if c <= b — the second starts no later than the first ends. I use intervals[i][0] <= newInterval[1] in the merge loop.



Frequently Asked Questions

What is the time complexity of the Insert Interval algorithm?

Because the problem enforces that the original intervals remain strictly sorted, an optimal linear scan algorithm completes the insertion in definitive O(N) execution time without requiring a secondary O(N log N) sorting step.

How does an optimal Insert Interval logic handle overlapping boundaries?

The core insight is checking when a new boundary overlaps an existing index (i.e. new_interval[0] <= current_interval[1]). You iteratively evaluate and extend the maximum bounds into a merged chunk before passing it onto the finalized array sequence.

Practice This in a Mock Interview

Start a mock interview for Insert Interval on Intervu.


Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →