Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [7, -3, 2, 5, -8, 4, 1, -1]. How many contiguous subarrays have a sum divisible by 3? (Zero is divisible by 3.)
- A. 12
- B. 9
- C. 14
- D. 10
Correct answer: D. 10
Prefix sums with the same remainder mod 3 give a divisible subarray between them. Remainders of negative values must be normalised before counting, and zero counts as divisible, which the empty-prefix seed takes care of.
More Coding Interview Problems questions
- Sorted list [3, 6, 9, 12, 15, 18, 21]. How many index triples i < j < k satisfy vals[i] + vals[j] = vals[k]?
- How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum to exactly 15?
- Array a = [9, 4, 7, 1, 8, 2, 6, 3]. How many index pairs i < j have |a[i] - a[j]| >= 5?
- Among [5, 3, 6, 12, 9, 10, 1], choose two different elements. What is the largest possible value of x OR y?
- How many pairs of distinct elements of [1, 4, 6, 7, 10, 13, 15] have a sum that is a perfect square (1, 4, 9, 16, ...)?
- Closed intervals [2,9], [4,7], [6,12], [10,14], [11,13], [15,18]. What is the total length of the set of points covered by at least one…