Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The union of the intervals [1, 5], [4, 8], [10, 12], [11, 15], [20, 22] covers some integer points. How many integers lie in the union (the endpoints are included)?

  1. A. 17
  2. B. 14
  3. C. 21
  4. D. 22

Correct answer: A. 17

Sorted merging gives [1, 8], [10, 15] and [20, 22], covering 8 + 6 + 3 = 17 integers. Using lengths e − s instead of e − s + 1 gives 14, adding the five intervals without merging gives 21, and the span 1..22 counts the gaps (22).

More Coding Interview Problems questions

Exams that ask this topic