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

How many pairs of distinct elements of [1, 4, 6, 7, 10, 13, 15] have a sum that is a perfect square (1, 4, 9, 16, ...)?

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

Correct answer: A. 3

Enumerate pairs and test the sum against a set of squares; the sum range is small here, so a set lookup is enough. Testing the product instead of the sum counts a different set of pairs.

More Coding Interview Problems questions

Exams that ask this topic