Maximum Size Subarray Sum Equals k
A medium-tier problem at 51% community acceptance, tagged with Array, Hash Table, Prefix Sum. Reported in interviews at Palantir Technologies and 0 others.
You need to find the longest subarray whose sum equals exactly k. Palantir Technologies asks this one, and it trips up candidates who default to brute force or don't recognize the prefix sum pattern. The naive approach checks every subarray, which is O(n²) and will time out. The trick is prefix sums plus a hash table: track cumulative sums and store their first occurrence. When you see the same prefix sum twice, you've found a zero-sum window. When current prefix minus k exists in the map, you've found a subarray equal to k. StealthCoder solves this in seconds if you blank on the hash table insight during your live assessment.
Companies that ask "Maximum Size Subarray Sum Equals k"
Maximum Size Subarray Sum Equals k is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe core pattern: use a hash map to store prefix_sum -> index. As you iterate, calculate cumulative sum at each position. For each position, check if (current_sum - k) exists in the map. If it does, the subarray between that stored index and the current index sums to exactly k. Track the maximum length. Initialize the map with {0: -1} so subarrays starting at index 0 are handled correctly. Most candidates miss that you need to store the FIRST occurrence of each prefix sum to maximize subarray length. The trap: modifying the map incorrectly or overwriting indices when you see a sum again. If you hit this live and freeze on the hash table mechanics, StealthCoder surfaces the solution invisible to the proctor.
Pattern tags
You know the problem.
Make sure you actually pass it.
Maximum Size Subarray Sum Equals k recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Size Subarray Sum Equals k interview FAQ
Why does the brute force O(n²) solution fail here?+
It does pass functionally, but online assessments have tight time limits. With n up to 10,000 or higher, O(n²) is too slow. The hash table approach is O(n) time and O(n) space, and it's the expected solution at Palantir and similar companies.
How do I avoid the off-by-one error with subarray length?+
Initialize your map with {0: -1}. This handles subarrays that start at index 0. Always store the FIRST occurrence of a prefix sum, never overwrite it. Length is current_index - map[prefix_sum - k], not current_index - map[prefix_sum - k] + 1.
What's the difference between this and subarray sum equals k without the 'maximum size' constraint?+
Without maximum size, you just count how many subarrays equal k. With it, you track length and keep the longest. The algorithm is almost identical; you just replace a counter with a max length variable and compare at each valid match.
Can negative numbers in the array break this approach?+
No. Negative numbers are actually crucial to this problem. They're why the hash table trick works: cumulative sums can match or repeat, creating zero-sum windows that wouldn't exist in all-positive arrays. The logic is identical.
Is this still asked at companies like Palantir, or is it too standard now?+
It appears in most interview reports for Palantir and similar mid-to-senior level assessments. It's not a rare problem, which means you're likely to see it. That's also why mastering the prefix sum plus hash table pattern matters for your OA prep.
Want the actual problem statement? View "Maximum Size Subarray Sum Equals k" on LeetCode →