Maximum Size Subarray Sum Equals K
Reported by candidates from eBay's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole eBay problem from September 2026 hinges on one hash map. Maximum Size Subarray Sum Equals K asks for the longest contiguous run that sums to k, and the array has negatives, so the usual sliding window falls apart. If you've got an OA invite, expect prefix sums stored in a map from running sum to first index. That's the answer. The code is about ten lines. Keep StealthCoder open as a safety net on the live OA in case your mind goes blank on the map setup, but the idea is simple enough to own tonight.
The problem
Given an integer array nums and an integer k, return the maximum length of a contiguous nonempty subarray whose elements sum to k. Return 0 if no such subarray exists. Function maxSubArrayLen(nums: int[], k: int) → int Examples Example 1 nums = [1,-1,5,-2,3] k = 3 return = 4 The subarray [1,-1,5,-2] sums to 3. Example 2 nums = [-2,-1,2,1] k = 1 return = 2 The subarray [-1,2] has length 2 and sum 1. Constraints 1 <= nums.length <= 2 * 10^5. Values and prefix sums fit in a 64-bit signed integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Track a running sum as you scan. At each index i, you want an earlier prefix equal to sum - k. If the map has it, the subarray from that index+1 to i sums to k, and its length is i minus the stored index. Store only the first index where each prefix sum appears, because earliest means longest. Seed the map with {0: -1} so subarrays starting at index 0 count. The classic pitfall is overwriting the stored index on repeats, which shrinks your answer. Another is trying two pointers, which breaks with negative numbers. Check example 1: prefixes hit 3 at index 4 with prefix 0 at -1... actually the sum 3 appears with sum-k=0 at -1 giving length 4 at index 2 or 3. Complexity is O(n) time and O(n) space. If you freeze during the live OA, StealthCoder can surface this map approach fast. Use a 64-bit type for sums.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Size Subarray Sum Equals K cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum size subarray sum equals k. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass eBay's OA.
eBay reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Size Subarray Sum Equals K FAQ
What's the trick for Maximum Size Subarray Sum Equals K?+
Use prefix sums with a hash map from prefix value to its first index. At each position, look up runningSum - k. If it exists, the candidate length is current index minus that stored index. Seed the map with 0 mapped to -1 so subarrays starting at the front are counted.
Why doesn't sliding window work here?+
Sliding window needs a monotonic sum, meaning growing the window always increases it. This array has negative values, so adding elements can lower the sum and shrinking can raise it. You can't decide which pointer to move. The prefix-sum map avoids that problem entirely.
How hard is this really for the eBay OA?+
Medium. The idea is short once you've seen it, and the code is tiny. The risk is edge cases: the 0 to -1 seed, keeping the first index only, and overflow. If you know those three things, you can finish it quickly.
Should I store the first or last index of each prefix sum?+
First. You want the longest subarray, so you want the earliest matching prefix. Only insert a prefix into the map if it isn't already there. Overwriting with later indices gives shorter subarrays and wrong answers on cases with repeated prefix sums.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice without looking. Then trace both examples by hand, including the prefix map contents. Then try variants: count of subarrays summing to k, and subarrays with sum divisible by k. They share the same prefix-sum skeleton, so you'll recognize them fast.