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.
- A. 6
- B. 7
- C. 5
- 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
- Array a = [3, 1, 4, 1, 5, 9, 2, 6]. How many subarrays of length exactly 3 have an even sum?
- Array a = [2, 4, 6, 1, 3, 5, 8, 7]. How many contiguous subarrays of length at least 2 have a sum divisible by 4?
- From the sorted side lengths [4, 6, 7, 9, 10, 13, 15], how many different triples (choosing distinct positions) can form a non-degenerate…
- Sorted arrays A = [1, 7, 12, 20, 31] and B = [3, 9, 15, 25, 40]. What is the smallest value of |x - y| over x in A and y in B?
- Values [3, 9, 4, 15, 8, 2, 11, 6] are given. Remove the fewest values so that the maximum minus the minimum of the remaining values is at…
- Sorted array a = [1, 2, 4, 7, 11, 16, 22, 29]. How many index pairs i < j have a[i] + a[j] <= 20?