ProgrammingModerateAmazon OA · Flipkart coding round

A solution must answer q = 2 * 10^5 queries of the form 'how many values in array A are at most x', with A of length n = 2 * 10^5 that never changes. Which Python approach is best?

  1. A. Build a new sorted list in every query using sorted(A)
  2. B. Use a list of all values, then call A.index(x) per query
  3. C. Sort A once, then use bisect_right per query for O(log n) each
  4. D. Call sum(v <= x for v in A) for each query

Correct answer: C. Sort A once, then use bisect_right per query for O(log n) each

Sorting once costs O(n log n), and bisect answers each query in O(log n), giving about O((n + q) log n) overall. Scanning A per query costs O(n q), which is too slow.

More Python Programming questions

Exams that ask this topic