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

For the array a = [4, 1, 7, 2, 2, 5, 3, 6], how many contiguous non-empty subarrays have XOR equal to 0? Use prefix XOR values.

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

Correct answer: C. 5

The XOR of a[i..j-1] is zero exactly when the prefix XORs at i and j are equal, so the answer is the number of equal pairs among the n+1 prefix values. Grouping the prefix values by equality and adding C(c, 2) for each group gives it directly.

More Coding Interview Problems questions

Exams that ask this topic