Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
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?
- A. 8
- B. 11
- C. 7
- D. 10
Correct answer: A. 8
Count pairs of prefixes with equal remainder mod 4 whose indices differ by at least 2. Including length-1 subarrays, which the question excludes, inflates the count by the number of single elements divisible by 4.
More Coding Interview Problems questions
- 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?
- How many permutations p of {0, 1, 2, 3, 4, 5} satisfy p[i] != i for every position i? Use a bitmask DP over the set of values already…
- Can the numbers 1 through 20 be split into two groups with equal sums? Answer 1 if yes and 0 if no, computed with a subset-sum DP over bits.