Data Structures & AlgorithmsModerateAccenture · Cognizant coding round pattern

Problem: n ≤ 10⁵ points on a line; answer 'how many points lie in [l, r]?' for 10⁵ queries. What is the simplest efficient solution?

  1. A. A hash set of points – O(1) per query
  2. B. Scan all points per query – O(n) each is fine
  3. C. Sort the points once; each query is bisect_right(r) − bisect_left(l) – O((n + q) log n)
  4. D. A max-heap of points

Correct answer: C. Sort the points once; each query is bisect_right(r) − bisect_left(l) – O((n + q) log n)

Two binary searches on the sorted array give the count. A hash set answers membership, not ranges. Scanning is 10¹⁰ in total.

More Coding Interview Problems questions

Exams that ask this topic