Data Structures & AlgorithmsVery hardGoogle · Flipkart interview

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 [6,8], [29,34], [22,29], [20,28], [8,15], what is the result?

  1. A. [6,15], [20,34]
  2. B. [20,34], [6,15]
  3. C. [6,8], [8,15], [20,28], [22,29], [29,34]
  4. D. [6,8], [8,15], [20,29], [29,34]

Correct answer: A. [6,15], [20,34]

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 [6,15], [20,34]. Requiring a strictly smaller start (< instead of ≤) would wrongly keep touching intervals separate, giving [6,8], [8,15], [20,29], [29,34].

More Coding Interview Problems questions

Exams that ask this topic