Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: A list of intervals is [1,5], [2,3], [4,8], [7,9], [10,11]. Merge all overlapping intervals (touching at an endpoint counts as overlapping). How many merged intervals are there?

  1. A. 2
  2. B. 3
  3. C. 1
  4. D. 4

Correct answer: A. 2

Sort by start and merge while the next start does not exceed the current end. Here [1,5],[2,3],[4,8],[7,9] collapse into [1,9], leaving [10,11] separate: two merged intervals.

More Coding Interview Problems questions

Exams that ask this topic