My first instinct was to treat it like a merge-intervals problem, which is basically right.
Clarify the problem: intervals are inclusive, and we need maximal gaps within the span from the minimum start to the maximum end. Sort intervals by start, merge overlapping or adjacent intervals, then compute gaps between merged intervals and at the boundaries. Return gaps sorted, which they naturally are if we process in order.
Pro tip: Explicitly discuss edge cases like empty input, single interval, and intervals that touch (e.g., [1,2] and [3,4] produce no gap if integers are discrete). Also mention that sorting dominates time complexity, so O(n log n) is optimal.
Confirm whether intervals are inclusive, whether touching intervals (e.g., [1,2] and [3,4]) should be merged, and what to return for empty or fully covered input. Discuss integer discreteness.
Sort the input intervals by their start value. This enables efficient merging and gap detection in a single pass.
Iterate through sorted intervals, merging any that overlap or are adjacent. Keep track of the current merged interval's end.
After merging, compute the gaps between consecutive merged intervals. Also check for gaps before the first interval and after the last interval within the overall span.
Collect all gaps as intervals and return them. Since we process in sorted order, the gaps are already sorted.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.