Skip to main content

92. Insert Interval

mediumAsked at Plaid

Insert a new interval into a sorted, non-overlapping list and merge if necessary. Plaid asks this because adding a new ETL-retry window to a sorted list of pre-existing windows is exactly this primitive.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid backend onsite — ETL window insert.
  • LeetCode Discuss (2026)Plaid SWE II OA.

Problem

You are given an array of non-overlapping intervals intervals where intervals[i] = [start_i, end_i] represent the start and the end of the i_th interval and intervals is sorted in ascending order by start_i. You are also given an interval newInterval = [start, end] that represents the start and end of another interval. Insert newInterval into intervals such that intervals is still sorted in ascending order by start_i and intervals still does not have any overlapping intervals.

Constraints

  • 0 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start_i <= end_i <= 10^5
  • intervals is sorted by start_i in ascending order.
  • newInterval.length == 2
  • 0 <= start <= end <= 10^5

Examples

Example 1

Input
intervals = [[1,3],[6,9]], newInterval = [2,5]
Output
[[1,5],[6,9]]

Example 2

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

Approaches

1. Insert sorted then merge

Insert at the right spot, then run LC 56's merge.

Time
O(n)
Space
O(n)
// Works but does more than needed.

Tradeoff: Two-step approach. Single-pass is cleaner.

2. Three-phase single pass

1) Push intervals strictly before newInterval. 2) Merge all overlapping with newInterval. 3) Push remaining intervals.

Time
O(n)
Space
O(n) output
function insert(intervals, ni) {
  const out = [];
  let i = 0;
  while (i < intervals.length && intervals[i][1] < ni[0]) out.push(intervals[i++]);
  while (i < intervals.length && intervals[i][0] <= ni[1]) {
    ni[0] = Math.min(ni[0], intervals[i][0]);
    ni[1] = Math.max(ni[1], intervals[i][1]);
    i++;
  }
  out.push(ni);
  while (i < intervals.length) out.push(intervals[i++]);
  return out;
}

Tradeoff: Linear, single pass, three clear phases. The phase boundaries (end < ni.start, start <= ni.end) classify each interval cleanly.

Plaid-specific tips

Plaid grades this on the three-phase structure. Bonus signal: name the three phases out loud (before, overlap, after). Connect to inserting a new ETL retry window into a sorted backlog while merging conflicts.

Common mistakes

  • Mutating newInterval in place — fine but document it.
  • Using > instead of >= in the overlap check (or vice versa) — depends on whether touching intervals merge.
  • Missing the final phase of pushing remaining intervals.

Follow-up questions

An interviewer at Plaid may pivot to one of these next:

  • Insert multiple intervals at once.
  • Insert into a non-sorted list.
  • Concurrent inserts — locking strategy.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why three phases?

Each interval is classifiable: it's strictly before newInterval, overlaps it, or is strictly after. Three loops with clear boundaries handle each cleanly.

Why mutate newInterval?

It's a free buffer for the merged result. If immutability matters, copy first.

Practice these live with InterviewChamp.AI

Drill Insert Interval and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →