Data Structures & AlgorithmsHardProduct Company SDE · hashing round
Problem: Count subarrays whose XOR is zero, for a = [1, 2, 3, 0, 3, 3].
Problem: Count subarrays whose XOR is zero, for a = [1, 2, 3, 0, 3, 3].
from collections import Counter
def cnt(a):
seen = Counter({0: 1})
cur = 0
ans = 0
for x in a:
cur ^= x
ans += seen[cur]
seen[cur] += 1
return ans
print(cnt([1, 2, 3, 0, 3, 3]))What is printed?- A. 4
- B. 7
- C. 6
- D. 3
Correct answer: B. 7
Equal prefix XOR values bound a zero-XOR subarray. Prefix values 0, 1, 3, 0, 0, 3, 0 give C(4,2) + C(2,2) = 6 + 1 = 7 pairs. Omitting the initial {0: 1} gives 4, skipping the single-element subarray [0] gives 6, and counting only subarrays that start at index 0 gives 3.
More Coding Interview Problems questions
- Problem: What does the following print? python print([format(i ^ (i >> 1), '03b') for i in range(4)])
- Problem: Count the pairs (i < j) with a[i] + a[j] < 6 for the array [5, 1, 4, 2, 3]. python def count(a, T): a = sorted(a) i, j = 0…
- Problem: Mo's algorithm answers q range queries on an array of length n by sorting queries into blocks of size about sqrt(n) and moving…
- Problem: In a segment tree with lazy propagation, what is the cost of one range-add update on an array of length n?
- Problem: What does the following print? python print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))
- Problem: What does the following print? python print(bin(12 & -12), 12 & -12)