Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

Given n = 5 × 10⁴ integers, count pairs i < j with a[i] > 2·a[j]. Which approach is correct and efficient?

  1. A. Modified merge sort: before merging two sorted halves, count with a two-pointer sweep, O(n log n)
  2. B. Sort the array, then use two pointers on the sorted copy, O(n log n)
  3. C. Prefix sums of the array plus binary search, O(n log n)
  4. D. A hash map of values seen so far, O(n)

Correct answer: A. Modified merge sort: before merging two sorted halves, count with a two-pointer sweep, O(n log n)

The condition depends on index order, so sorting the whole array first destroys the i < j information. Merge sort keeps each half's original indices on one side, so for every element of the right half you count left-half values > 2·a[j] with a moving pointer before merging. A hash map cannot answer an inequality query.

More Coding Interview Problems questions

Exams that ask this topic