Data Structures & AlgorithmsVery hardIBM · Deloitte pattern

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 [4,11], [11,19], [1,6], [2,10], [15,20], [26,33], [28,36], what is the result?

  1. A. [1,11], [11,20], [26,36]
  2. B. [1,6], [2,10], [4,11], [11,19], [15,20], [26,33], [28,36]
  3. C. [26,36], [1,20]
  4. D. [1,20], [26,36]

Correct answer: D. [1,20], [26,36]

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 [1,20], [26,36]. Requiring a strictly smaller start (< instead of ≤) would wrongly keep touching intervals separate, giving [1,11], [11,20], [26,36].

More Coding Interview Problems questions

Exams that ask this topic