Code Room
CodingMediumcod-g667
Subject IntervalsLevel Mid–Senior~22 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You are given `intervals`, a list of non-overlapping intervals [start, end] sorted ascending by start, and a single `new_interval` [start, end]. Insert the new interval and merge as needed so the result remains sorted and non-overlapping. Return the new list. Two intervals overlap (and merge) if they touch — i.e. [1,2] and [2,3] merge into [1,3]. Up to 10^4 intervals; aim for O(n) with a single pass.

Implement
insert_interval(intervals: list[list[int]], new_interval: list[int]) → list[list[int]]
Examples
in[[[1,3],[6,9]],[2,5]]out[[1,5],[6,9]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.