Shortest Subarray With a Target Remainder
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's September 2026 OA has a problem that looks like a standard prefix-sum remainder question, and then punishes the first version you write. You're asked for the shortest nonempty subarray whose sum hits remainder k modulo 70001, with negatives allowed and a million elements. The pattern is prefix sums plus a hash-style lookup, here a fixed array of size 70001. If you blank on the setup, StealthCoder is the invisible safety net that reads the problem and gives you the working solution live. Know the trick before you open the invite anyway.
The problem
You are given an integer array nums and a target remainder k. The modulus is the fixed constant 70001. Return the length of the shortest nonempty contiguous subarray whose element sum has remainder k modulo 70001. Return -1 if no such subarray exists. Values may be negative. Treat every remainder canonically in the range 0 through 70000. Process the values from left to right using O(70001) auxiliary space rather than storing a prefix record for every array position. Function shortestSubarrayRemainder(nums: int[], k: int) → int Examples Example 1 nums = [2,3,1,2] k = 6 return = 3 Both [2,3,1] and [3,1,2] have sum 6. No qualifying subarray has length 1 or 2, so the shortest length is 3. Example 2 nums = [5,-2,4,-6,3] k = 2 return = 2 The subarray [-2,4] has sum 2. No single value has remainder 2, so the answer is 2. Example 3 nums = [70000,1,70001] k = 0 return = 1 The one-element subarray [70001] has remainder 0, so no longer qualifying subarray can improve the answer. Example 4 nums = [2,4,8] k = 7 return = -1 No nonempty contiguous subarray has a sum congruent to 7 modulo 70001. Constraints 1 <= nums.length <= 10^6. -10^9 <= nums[i] <= 10^9. 0 <= k < 70001. The answer must use a nonempty contiguous subarray. Use O(nums.length) time and O(70001) auxiliary space.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a running prefix remainder, always normalized into 0 through 70000. A subarray ending at index i has remainder k exactly when prefix[i] minus prefix[j] is congruent to k, so you look up (prefix[i] - k) mod 70001 in an array that stores the latest index where each remainder appeared. Latest index gives the shortest length. The edge case that breaks the naive version is the empty prefix. Seed the array with remainder 0 at index -1, or a subarray starting at position 0 gets missed. The second trap is negatives: in most languages the % operator returns negative values, so normalize with ((x % m) + m) % m. Update the array after the lookup, not before, or k = 0 returns length 0 for an empty subarray. Example 3 shows it: the single element 70001 gives answer 1. StealthCoder is your hedge if the normalization or seeding slips under the clock. Time is O(n), space is O(70001).
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Shortest Subarray With a Target Remainder 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Subarray With a Target Remainder FAQ
What's the trick in the Google shortest subarray remainder problem?+
Prefix remainders with a last-seen-index array of size 70001. At each position, look up the remainder (current - k) mod 70001 and compute the distance to that stored index. Because you store the latest index, the length is minimal. Then record the current remainder. It's one pass, O(n) time.
Why does k = 0 cause so many wrong answers?+
The lookup finds the current prefix itself if you update the array before querying. That produces length 0, which isn't allowed since the subarray must be nonempty. Always query first, then store the current index. Example 3 with 70001 as the last element should return 1, not 0.
How do I handle negative numbers and the modulus?+
Normalize every remainder into 0 through 70000 using ((x % 70001) + 70001) % 70001. Values go down to -10^9, and languages like Java, C++, and JavaScript return negative results from %. Skip normalization and your array index goes negative or you match the wrong remainders.
Do I need to seed the array with an empty prefix?+
Yes. Set remainder 0 to index -1 before processing. Without it, any valid subarray that starts at index 0 is never found. Example 2 won't break, but an input like [2,3,1] with k = 6 would return -1 instead of 3.
How do I prepare for this in 48 hours?+
Write the prefix-remainder lookup from scratch twice, once with a hash map and once with a fixed array. Test it on the four examples plus a single-element case and an all-negative array. Check that you query before updating. That covers nearly every way this problem fails.