Data Structures & AlgorithmsHardTCS Digital · Infosys SP

The intervals [4, 8], [13, 15], [5, 8], [8, 11], [12, 16], [8, 11] 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. 2 intervals, length 19
  2. B. 5 intervals, length 19
  3. C. 2 intervals, length 11
  4. D. 3 intervals, length 11

Correct answer: C. 2 intervals, length 11

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

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

Exams that ask this topic