Answer: Salesforce Associate Developer Interview Question 2024 April | Prefix Sum | on-c

Answer · Posted Jun 2026

Solution 2: Subarray Sum Equals K — HashMap Prefix Sum Approach Instead of checking every subarray in O(N2) time, we use a HashMap to track the frequency of prefix sums. This allows us to find subarrays in linear time. Let prefixSum[i] be the sum of elements from index 0 to i. The sum of a subarray from index j to i is prefixSum[i] - prefixSum[j-1]. We want this sum to equal k: prefixSum[j-1] = prefixSum[i] - k. As we iterate ...

The full answer & interview discussion are available to premium members.

Log in Create a free account