Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft

How many contiguous subarrays of [4, 2, 2, 6, 4, 1, 5] have bitwise XOR equal to 6? (Prefix XORs with a frequency map: add freq[px ^ 6] at each step, seeding freq[0] = 1.)

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

Correct answer: B. 6

Since px[j] ^ px[i] is the XOR of a[i..j − 1], each earlier prefix equal to px ^ 6 ends a valid subarray. The total is 6 (the number of subarrays with SUM 6 is 3, a different question).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic