Skip to main content

93. Insert Interval

hardAsked at Salesforce

Insert a new interval into a sorted, non-overlapping list and merge as needed. Salesforce uses this directly in their Calendar app's event insertion.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce Calendar event-insertion uses this exact pattern.
  • Blind (2025-11)Recurring on Salesforce L4/L5 onsites.

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 ith 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 (merge overlapping intervals if necessary). Return intervals after the insertion.

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. Concatenate, sort, merge

Add newInterval, sort, run merge-intervals.

Time
O(n log n)
Space
O(n)
// Works but doesn't exploit sortedness — O(n log n) vs achievable O(n).

Tradeoff: Wastes the sorted input.

2. Single pass: three phases

1. Add intervals entirely before newInterval. 2. Merge all overlapping with newInterval. 3. Add intervals entirely after.

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

Tradeoff: Linear time. The three-phase structure is the textbook approach.

Salesforce-specific tips

Salesforce uses this exact algorithm in their Calendar app — when you drag-and-drop a new event onto a calendar, it merges with conflicting existing events. They grade on whether you handle all three phases cleanly: before, overlap, after. Bonus signal: mention that mutating newInterval during the merge loop is fine because the input said it could be modified.

Common mistakes

  • Mixing the three phases — leads to convoluted code that's hard to debug.
  • Using `intervals[i][1] <= newInterval[0]` (with equality) — touching intervals should merge.
  • Forgetting to push newInterval after the merge loop.

Follow-up questions

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

  • Merge Intervals (LC 56).
  • Meeting Rooms II (LC 253).
  • Remove Interval — complement operation.

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?

Because the input is sorted and the new interval has a definite position. Phase 1 handles ahead-of-new, phase 2 handles overlapping, phase 3 handles after.

Why mutate newInterval?

Convenience — it accumulates the merged bounds. If immutability is required, use local variables.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →