Count Paired Subarrays Reaching a Target
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ZipRecruiter OA reported in December 2023 looks like a two-array puzzle, but the trap is in how you count. You pick one subarray from each array and need their sums to hit the target. Count by sums, not by distinct values, or Example 1 gives you the wrong answer. It's a hash-table and prefix-sum problem in disguise, and it's easy to overthink under a clock. If you blank on the counting step, StealthCoder runs invisibly on your screen during the live OA and can hand you the approach. Here's the script before you need it.
The problem
Given positive integer arrays first and second and a positive target, choose one nonempty contiguous subarray from each array. Return the number of ordered choices whose two subarray sums add to target. Function countPairedSubarrays(first: int[], second: int[], target: int) → long Examples Example 1 first = [1,2] second = [1,1] target = 3 return = 3 First-array sums 1 and 2 pair with second-array sums 2 and 1 respectively, producing three index-range choices. Example 2 first = [5] second = [5] target = 10 return = 1 The two singleton subarrays form one choice. Constraints 1 <= first.length,second.length <= 1000 1 <= value <= 1000 1 <= target <= 1000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: all values are positive and lengths are at most 1000, so each array has at most about 500,000 subarrays. Compute every subarray sum of first and count them in a hash map. Then compute every subarray sum s of second and add map[target - s]. The edge case that breaks naive solutions is deduplicating sums. Different index ranges with the same sum are different choices, so you must count multiplicities, not distinct sums. Example 1 shows it: sum 2 from first pairs with sum 1 from second, which appears twice. Also use a 64-bit counter, since the answer can reach roughly 250 billion. Skip the O(n^2 * m^2) brute force. Prefix sums make each subarray sum O(1), giving O(n^2 + m^2) time overall. If the live OA freezes you on the map lookup, StealthCoder is your hedge, but you can write this in ten lines.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Paired Subarrays Reaching a Target 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
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter 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.
Count Paired Subarrays Reaching a Target FAQ
What's the trick in the ZipRecruiter paired subarrays problem?+
Count subarray sums of the first array in a hash map, then for each subarray sum of the second array, add the count of target minus that sum. Don't pair subarrays directly. Pairing sums by frequency cuts a four-loop brute force down to two quadratic passes.
Why does the answer need a long?+
Each array can have around 500,000 subarrays at length 1000. In the worst case, many pairs match, so the count can reach hundreds of billions. That overflows a 32-bit int, so use a 64-bit integer in your language.
Do I count distinct sums or every index range?+
Every index range. Two different subarrays with the same sum are separate choices. That's why you store frequencies in the map, not a set. Example 1 returns 3 because a sum of 1 appears twice in the second array.
Can I use sliding window here?+
Not needed. Sliding window helps when you search for one target inside a single array. Here you want all subarray sums of both arrays, and with 1000-length limits, enumerating them with prefix sums or running totals is fast enough.
How do I prepare in 48 hours for this kind of OA question?+
Write this solution from memory twice. Focus on enumerating subarray sums with a running total, using a frequency map, and the complement lookup. Then test Example 2, a single element each, to confirm that nonempty subarrays and the 64-bit count work.