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?- A. 5
- B. 4
- C. 10
- 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
- For the string "character", find the length of its longest palindromic subsequence (characters need not be adjacent, but must keep their…
- Count the subarrays of w = [1, 3, 2, 5, 4, 4, 2] whose maximum minus minimum is at most 3. Run the brute-force count. python…
- What is the minimum number of perfect squares (1, 4, 9, 16, ...) whose sum is exactly 12? Run the DP over all squares not exceeding 12…
- Count the subsequences (by position, not distinct strings) of "abcabc" that equal "abc". Run the DP that counts matches. python…
- Array a = [2, 3, 1, 1, 4, 2, 0, 3, 1, 1]; from index i you may jump to any index in [i+1, i+a[i]]. Find the minimum number of jumps from…
- Trapping rain water: heights h = [4, 2, 0, 3, 2, 5, 1, 0, 2]. Compute the total water trapped between the bars. python…