Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

Array a = [3, 5, 8, 2, 7, 10, 4]. How many pairs of positions i < j have a[i] + a[j] equal to some value located strictly after position j?

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

Correct answer: B. 4

For each pair, check the sum against the suffix after j, using a set of values in that suffix to make each test O(1). Checking values before j instead answers a different question.

More Coding Interview Problems questions

Exams that ask this topic