Determine if a multiple-sum subarray exists
Company: Adobe
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: 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.
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.