Data Structures & AlgorithmsHardLeetCode-style · TCS NQT

Closed real intervals [(1, 4), (3, 6), (8, 9), (9, 12), (14, 15)] are merged whenever they overlap (sharing an endpoint counts as overlap). What is the total length covered by the union?

  1. A. 11
  2. B. 10
  3. C. 14
  4. D. 3

Correct answer: B. 10

Sort and merge: [1,4] and [3,6] become [1,6]; [8,9] and [9,12] share 9 and become [8,12]; [14,15] stays. Lengths 5 + 4 + 1 = 10. Summing the originals double-counts [3,4] (11), 14 is the span 1 to 15, and 3 is the number of blocks.

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

Exams that ask this topic