Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching endpoints count as overlapping) on [2,5], [26,32], [8,10], [10,15], [0,2], [9,14], what is the result?
- A. [0,5], [8,15], [26,32]
- B. [0,2], [2,5], [8,15], [26,32]
- C. [0,2], [2,5], [8,10], [9,14], [10,15], [26,32]
- D. [26,32], [8,15], [0,5]
Correct answer: A. [0,5], [8,15], [26,32]
After sorting by start time, two intervals merge whenever the next one's start is ≤ the current merged end (so [2,5] and [5,8] do merge, since they touch at 5); applying this gives [0,5], [8,15], [26,32]. Requiring a strictly smaller start (< instead of ≤) would wrongly keep touching intervals separate, giving [0,2], [2,5], [8,15], [26,32].
More Coding Interview Problems questions
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- What is the length of the longest common subsequence of "DADADEECA" and "BADABAD"?
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…