Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Array a = [-2, 3, -4, 6, -1, 2]. How many non-empty subarrays have a sum divisible by 5? Count prefix sums by their remainder, normalised to 0..4 as in Python.
- A. 0
- B. 1
- C. 2
- D. 5
Correct answer: C. 2
Two prefix sums with the same non-negative remainder mod 5 mark a divisible subarray. In C or Java, % on a negative prefix returns a negative remainder, which splits equal classes; that version gives 0 instead of 2.
More Coding Interview Problems questions
- In the binary string s = "1100110100", count the non-empty substrings that contain exactly as many 0s as 1s.
- Array a = [2, 7, -3, 5, 11, 4, 9, -1, 6]. How many index pairs (i, j) with i < j have a[i] + a[j] = 10?
- Array a = [2, 1, -1, 4, -2, 3, 0, 1, 1, 0, 2]. How many indices i have the sum of the elements strictly left of i equal to the sum of the…
- Array a = [3, 1, 2, 6, 4, 5, 9]. How many index pairs (i < j) satisfy a[i] mod 3 = a[j] mod 3?
- Sorted array a = [-4, -1, 0, 2, 3, 5, 7]. How many triples of distinct indices i < j < k have a[i] + a[j] + a[k] <= 6?
- For the positive array a = [2, 3, 1, 2, 4, 3] and S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?