94. Insert Interval
hardAsked at SnowflakeInsert a new interval into a sorted list of non-overlapping intervals, merging where needed. Snowflake asks this for streaming-friendly interval management — relevant to incremental micro-partition coalescing during continuous ingestion.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake clustering team uses this in onsites.
- LeetCode Discuss (2025-11)— Reported at Snowflake SDE-II screens.
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 (merge overlapping intervals if necessary). Return intervals after the insertion.
Constraints
0 <= intervals.length <= 10^4intervals[i].length == 20 <= start_i <= end_i <= 10^5intervals is sorted by start_i in ascending order.newInterval.length == 2
Examples
Example 1
intervals = [[1,3],[6,9]], newInterval = [2,5][[1,5],[6,9]]Example 2
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8][[1,2],[3,10],[12,16]]Approaches
1. Append and re-merge
Append newInterval, then re-run the LC 56 Merge Intervals algorithm.
- Time
- O(n log n)
- Space
- O(n)
// outline only — works but does a full sort.Tradeoff: Throws away the fact that intervals is already sorted.
2. Three-phase single pass (optimal)
Phase 1: copy intervals fully before newInterval. Phase 2: merge overlapping with newInterval. Phase 3: copy remaining.
- Time
- O(n)
- Space
- O(n) output
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, single pass. Leverages the sortedness fully.
Snowflake-specific tips
Snowflake interviewers want the three-phase pattern explicitly. Bonus signal: connect to continuous ingestion — when streaming ingestion adds new micro-partitions, the coalescer must merge them with overlapping existing partitions in a similar three-phase manner.
Common mistakes
- Using strict < and > inconsistently — be precise about touching intervals.
- Merging when not overlapping (newInterval still left of next interval).
- Mutating newInterval and reusing it without copying — fine here but a gotcha in general.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Merge Intervals (LC 56) — the underlying primitive.
- Remove Interval (LC 1272).
- How does Snowflake coalesce streaming-ingested partitions?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why three phases?
It's the natural structure: things that finish before newInterval (skip), things that overlap (merge), things that start after (skip). Each phase advances i monotonically.
How does this connect to ingestion?
Streaming ingestion adds new partitions with [min, max] ranges. The coalescer merges overlapping ranges in real time — this problem is the in-memory equivalent.
Practice these live with InterviewChamp.AI
Drill Insert Interval and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →