Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [3, -1, 2, 6, -4, 5, 1, -2]. How many non-empty contiguous subarrays have a sum divisible by 4?
- A. 10
- B. 12
- C. 7
- D. 26
Correct answer: A. 10
Prefix sums sharing the same remainder mod 4 bound a subarray with a sum divisible by 4. Count how many prefixes share each remainder, starting with the empty prefix, and add C(count, 2) over the groups in a single pass.
More Coding Interview Problems questions
- Binary array a = [1, 0, 1, 1, 0, 0, 1, 0, 1]. How many contiguous subarrays have exactly two 1s?
- Array a = [4, 2, 7, 1, 8, 3, 6, 5]. What is the largest sum of a contiguous subarray whose length is between 2 and 4 inclusive?
- In the string "abcabcaab", count the non-empty substrings that contain equally many vowels and consonants (vowels are a, e, i, o, u).
- For the array a = [1, 4, 7, 2, 5, 6, 3], how many index pairs i < j satisfy a[i] XOR a[j] = 5?
- Array a = [2, -1, 3, 1, -2, 4]. How many contiguous subarrays have a sum equal to their length? Subtract 1 from each element first and…
- Sorted arrays A = [1, 5, 9, 12] and B = [3, 4, 10, 15, 20]. How many pairs (x in A, y in B) satisfy |x - y| <= 2?