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

Arrays A = [1, 2], B = [-2, -1], C = [-1, 2] and D = [0, 2] each have two entries. How many index quadruples (i, j, k, l) satisfy A[i] + B[j] + C[k] + D[l] = 0? Use a meet-in-the-middle count of pair sums.

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

Correct answer: C. 2

Put the sums A+B into a counter and, for each pair from C and D, look up the negated sum and add its multiplicity. The count is over index quadruples (2); collecting distinct sum values gives only 1.

More Coding Interview Problems questions

Exams that ask this topic