Determine if a multiple-sum subarray exists
Company: Adobe
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Determine if a multiple-sum subarray exists states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= nums.length is typical, but the function must also handle an empty array (returns false).
- Elements of nums may be negative, zero, or positive.
- k may be negative, zero, or positive; for k != 0 use |k| when testing divisibility.
- When k = 0, a single element equal to 0 does NOT qualify — the subarray must have length >= 2.
Examples
Input: ([23, 2, 4, 6, 7], 6)
Expected Output: True
Explanation: Subarray [2, 4] (indices 1..2) sums to 6, a multiple of 6.
Input: ([23, 2, 6, 4, 7], 6)
Expected Output: True
Explanation: Subarray [6, 4] sums to 10... but [2, 6, 4] sums to 12, which is a multiple of 6.
Input: ([23, 2, 6, 4, 7], 13)
Expected Output: False
Explanation: No contiguous subarray of length >= 2 has a sum that is a multiple of 13.
Input: ([1, 2, 3], 5)
Expected Output: True
Explanation: Subarray [2, 3] sums to 5, a multiple of 5.
Input: ([5], 5)
Expected Output: False
Explanation: Only one element, so no subarray of length >= 2 exists.
Input: ([], 1)
Expected Output: False
Explanation: Empty array has no subarray of length >= 2.
Input: ([0, 0], 0)
Expected Output: True
Explanation: k = 0: subarray [0, 0] has length 2 and sums to exactly 0.
Input: ([1, -1, 3], 0)
Expected Output: True
Explanation: k = 0: subarray [1, -1] sums to exactly 0.
Input: ([1, 2, 3], 0)
Expected Output: False
Explanation: k = 0: no contiguous subarray of length >= 2 sums to exactly 0.
Input: ([0, 1, 0], 0)
Expected Output: False
Explanation: k = 0: each 0 is a single element; no length-≥2 subarray sums to 0 (the only zero-sum windows have length 1).
Input: ([-1, 2, 9], -3)
Expected Output: False
Explanation: Using |k| = 3: subarray sums are 1, 11, 10 (length >= 2); none is a multiple of 3.
Input: ([6, 0], 6)
Expected Output: True
Explanation: Subarray [6, 0] sums to 6, a multiple of 6.
Hints
- A subarray sum equals prefixSum[j] - prefixSum[i]. It is divisible by k exactly when prefixSum[j] and prefixSum[i] leave the same remainder mod |k| (or are equal, when k = 0).
- Store the FIRST index at which each remainder (or prefix value) is seen, and require the gap between matching indices to be at least 2 to enforce the length-≥2 rule.
- Seed the map with remainder 0 at index -1 so a prefix that is itself divisible by k (a subarray starting at index 0) is detected.
- Handle k = 0 separately: track raw prefix sums instead of remainders and look for two equal prefix sums at least 2 indices apart.