Data Structures & AlgorithmsVery hardAmazon · Google coding round
For the array [4, 5, 0, -2, -3, 1, 7, -9, 6] and k = 5, how many non-empty contiguous subarrays have a sum divisible by k?
- A. 11
- B. 14
- C. 9
- D. 12
Correct answer: D. 12
Count prefix sums with equal remainder mod 5 (normalised to be non-negative) and seed the count of remainder 0 with 1 for the empty prefix. This gives 12; omitting the seed gives 11.
More Coding Interview Problems questions
- How many pairs of indices (i, j) with i < j satisfy a[i] > 2·a[j] in a = [5, 2, 6, 1, 7, 3, 9, 2, 4]?
- A walker moves from (0,0) to (4,4) on lattice points using steps (1,0), (0,1) and (1,1). The point (2,2) is forbidden. How many different…
- How many permutations of {1, 2, …, 8} have exactly two fixed points (positions i with π(i) = i)?
- How many integers n with 1 ≤ n ≤ 999 have digit sum exactly 12 and contain no digit 0 (in their usual decimal form, without leading zeros)?
- Find the length of the longest common subsequence of X = "AGGTABQXZRYWM" and Y = "GXTXAYBZQMWRY".
- What is the minimum number of characters that must be inserted anywhere in "CBAABDCABBDAC" to make it a palindrome?