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

Closed intervals [1,6], [4,10], [5,5], [8,12], [2,3] are given. For the query points 5, 9 and 1, count how many intervals contain each point, then add the three counts. What is the total?

  1. A. 7
  2. B. 6
  3. C. 4
  4. D. 3

Correct answer: B. 6

Sort the starts and ends separately and use binary search to count intervals with start at most q minus those with end below q. Counting only intervals that contain the first query misses the others.

More Coding Interview Problems questions

Exams that ask this topic