Code Room
CodingMediumcod-g1468
Subject IntervalsLevel Entry–Mid~14 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an unsorted list of closed intervals [start, end] and a nonnegative gap g, repeatedly merge any two intervals whose distance is at most g, where overlapping intervals have distance 0 and disjoint ones are separated by the space between them. Keep merging until no pair qualifies, then return the resulting intervals sorted by start. Example: [[1,3],[6,8]] with g = 3 gives [[1,8]], but with g = 2 stays [[1,3],[6,8]].

Implement
merge_with_gap(intervals: list[list[int]], g: int) → list[list[int]]
Examples
in[[[1,3],[6,8]],3]out[[1,8]]
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.