Data Structures & AlgorithmsHardGATE CS · DSA

The intervals [15, 16], [11, 16], [4, 9], [4, 8], [8, 12], [20, 22] are sorted by start and merged (intervals that touch, like [2, 4] and [4, 6], are merged too). How many intervals remain, and what is the total length covered?

  1. A. 3 intervals, length 14
  2. B. 3 intervals, length 15
  3. C. 2 intervals, length 21
  4. D. 2 intervals, length 14

Correct answer: D. 2 intervals, length 14

After sorting by start, extend the last merged interval while the next start ≤ its end. Result: [4, 16], [20, 22] — 2 interval(s) covering 14 units (summing the raw lengths double-counts overlaps).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic