Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

The half-open intervals [0,3), [2,5), [4,8) and [7,9) are given. What is the total length of their union (the number of unit-length cells [x, x+1) for integers x covered by at least one interval)?

  1. A. 12
  2. B. 11
  3. C. 9
  4. D. 10

Correct answer: C. 9

Sort by start and merge overlapping intervals, adding only the new part of each. Summing lengths gives 12 because [2,3) and [4,5) are covered twice; the merged union is [0,9), so the answer is 9.

More Coding Interview Problems questions

Exams that ask this topic