Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Array a = [4, 2, 6, 1, 5, 3, 3, 9, 2]. How many contiguous subarrays of length at least 3 have a sum divisible by 6?
- A. 6
- B. 8
- C. 5
- D. 11
Correct answer: A. 6
Prefix sums with the same remainder mod 6 give divisible subarrays. The length restriction cannot be checked by remainders alone, so a window of length at least 3 is enforced by inserting prefix indices only once they are 3 or more steps behind the current end.
More Coding Interview Problems questions
- Array a = [3, 5, 8, 2, 7, 10, 4]. How many pairs of positions i < j have a[i] + a[j] equal to some value located strictly after position j?
- Sorted array b = [1, 2, 3, 4, 5, 7, 8, 9]. What is the largest product b[i] b[j] over index pairs with j - i >= 3?
- Sorted array [1, 3, 4, 6, 8, 9, 11]. How many index triples i < j < k satisfy c[k] - c[i] <= 4?
- How many strings of length 5 over the alphabet {a, b, c} have no two adjacent characters equal?
- How many non-empty subsets of {1, 2, 3, 4, 5, 6} have an XOR of their elements that is at least 4?
- How many subsets (including the empty subset) of the values [2, 3, 5, 7, 11, 13, 4, 6] have a sum between 10 and 20 inclusive?