Data Structures & AlgorithmsModerateTCS CodeVita · Microsoft SDE OA

For the array c = [1, 2, 3, 4], consider every non-empty subarray and its XOR value. How many subarrays have an XOR value that is strictly greater than 4? Run the code.

For the array c = [1, 2, 3, 4], consider every non-empty subarray and its XOR value. How many subarrays have an XOR value that is strictly greater than 4? Run the code.
c=[1,2,3,4]
n=len(c); cnt=0
for i in range(n):
    x=0
    for j in range(i,n):
        x^=c[j]
        if x>4: cnt+=1
print(cnt)
What is printed?
  1. A. 5
  2. B. 4
  3. C. 10
  4. D. 2

Correct answer: D. 2

Accumulate XOR along each subarray and test the result directly. Counting all subarrays ignores the condition, and using the sum instead of the XOR changes the values checked.

More Coding Interview Problems questions

Exams that ask this topic