Merge overlapping and adjacent ranges
AmazonSoftware Engineer · OnsiteLast updated
This question evaluates a candidate's understanding of interval merging and range manipulation, including handling overlapping and adjacent ranges, edge cases like single-point gaps and extreme values, and the ability to analyze time and space complexity.
Given an unsorted list of integer ranges represented as half-open intervals [start, end) with start < end, merge all overlapping or directly adjacent ranges and return the merged list sorted by start. Also return the total length covered by the merged ranges. For example, merging [1, 4), [3, 5), and [5, 7) yields [1, 7) and total length 6. Implement an efficient solution and analyze time and space complexity. Consider edge cases such as single-point gaps, negative values, very large inputs, and stability of the original ordering when ranges are identical.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.