Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Intervals

Insert Interval

MEDIUM✓ CHIP-TIMEDLC #57 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: A calendar already holds non-overlapping meetings sorted by start time. Drop one more meeting in, merging it with anything it touches, and hand back the calendar in order.

A calendar of meetings arrives already sorted by start time, with no two meetings overlapping each other. One additional meeting needs to be dropped into that calendar.

Wherever the new meeting overlaps or touches an existing one, those meetings merge into a single wider block covering their combined span — this can chain across several existing meetings at once if the new one bridges them.

The result is the full calendar again, still sorted by start time and still with no two entries overlapping, reflecting the new meeting's insertion and any merges it caused.

  • existing meetings arrive already sorted by start time, non-overlapping
  • the new meeting can overlap, touch, or sit apart from any of them
  • touching intervals (end equals next start) count as overlapping and merge
  • the returned calendar stays sorted and fully non-overlapping
EX 01
intervals = [[1, 4], [8, 10]] · newInterval = [5, 7]
→ [[1, 4], [5, 7], [8, 10]]
new interval fits in the gap, no merge
EX 02
intervals = [[2, 4], [6, 8], [10, 12]] · newInterval = [5, 7]
→ [[2, 4], [5, 8], [10, 12]]
overlaps only the middle interval
EX 03
intervals = [] · newInterval = [3, 5]
→ [[3, 5]]
empty calendar

The hintstake only what you need

HINT 1 THE NUDGE

The intervals well before the new one's reach and the ones well after it never change — only the ones the new interval overlaps need any work. What three groups is the array splitting into?

HINT 2 THE STRUCTURE

Walk left to right: intervals ending strictly before the new one starts pass through untouched, intervals starting strictly after the new one ends pass through untouched, and everything else — the new interval included — belongs to one merge.

HINT 3 ONE STEP FROM THE ANSWER

Copy the untouched-left intervals as-is, absorb every overlapping interval into the new one by widening its start and end, then append the untouched-right intervals.

Coach's boardthe pattern, step by step

THE ONE-PASS SPLICEPATTERN · GREEDY — LINEAR SWEEPintervals = [[2,4],[6,8],[10,12]] · newInterval = [5, 7]
[2,4]
[6,8]
[10,12]
RESULT / MERGING WINDOW
result → []
new → [5, 7]
STEP 1

Drop [5, 7] into the sorted calendar [[2,4],[6,8],[10,12]]. Untouched-left, then absorb overlaps, then untouched-right.

STEP 1 / 6 · ← → WORK TOO

The splitstwo paces, two languages

grind/insert-interval.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        result = []
        i, n = 0, len(intervals)
        start, end = newInterval

        while i < n and intervals[i][1] < start:
            result.append(intervals[i])
            i += 1

        while i < n and intervals[i][0] <= end:
            start = min(start, intervals[i][0])
            end = max(end, intervals[i][1])
            i += 1
        result.append([start, end])

        while i < n:
            result.append(intervals[i])
            i += 1

        return result
TIME O(N)SPACE O(N)PYTHON · RACE PACE · 21 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED