Code RoomMerge intervals with gap
MediumPrep Room Coding #669

Merge intervals with gap

CodingAlgorithms & data structuresEntry–Mid~14 min

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.

0:00 of about 14 min
InputExpectedGot
[[[1,3],[6,8]],3][[1,8]]not run yetsample