Count Subarrays Matching a Comparison Pattern
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this ZipRecruiter problem, reported in September 2024, is the window length: pattern.length + 1, not pattern.length. Miss that and every count is off. You get an array of up to 10^5 numbers and a pattern of -1, 0 and 1, and you count windows whose adjacent comparisons match exactly. It's string matching wearing a number costume. If you have the OA in a day or two, learn the conversion trick below. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is very learnable.
The problem
For this exercise, use the callable contract below. You are given an integer array numbers and an integer array pattern containing only -1, 0, and 1. Each pattern value describes one adjacent comparison: 1 means the next value is greater. 0 means the next value is equal. -1 means the next value is smaller. Return the number of contiguous subarrays of length pattern.length + 1 whose adjacent comparisons exactly match the complete pattern. Function countMatchingSubarrays(numbers: int[], pattern: int[]) → int Examples Example 1 numbers = [1,2,3,4,5,6] pattern = [1,1] return = 4 Every length-three window is strictly increasing, so all four candidate windows match. Example 2 numbers = [1,4,4,1,3,5,5,3] pattern = [1,0,-1] return = 2 The windows [1,4,4,1] and [3,5,5,3] increase, stay equal, and then decrease. Constraints 2 <= numbers.length <= 10^5 1 <= pattern.length < numbers.length -10^9 <= numbers[i] <= 10^9 pattern[i] is -1, 0, or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: convert numbers into a difference array of length n-1, where each entry is the sign of numbers[i+1] - numbers[i]. Now the problem is counting occurrences of pattern inside that sign array. The brute force compares each window against the pattern, which is O(n*m) and can time out with n at 10^5 and a long pattern. Use KMP (prefix function) or Z-function for O(n+m). A rolling hash also works but KMP is safer, with no collisions to worry about. The common pitfalls are using subtraction without taking the sign, forgetting that equal values map to 0, and off-by-one on the window size. Check Example 1: 6 numbers give 5 signs, pattern length 2, so 4 matches. If the live OA makes you blank on KMP, StealthCoder can hand you the working solution while you keep control of the clock.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Subarrays Matching a Comparison Pattern 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
This OA pattern shows up on LeetCode as number of subarrays that match a pattern i. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter 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.
Count Subarrays Matching a Comparison Pattern FAQ
What's the trick to this ZipRecruiter pattern-matching problem?+
Turn the numbers array into an array of signs (-1, 0, 1) from adjacent differences. Then the task becomes counting how many times the pattern appears as a contiguous block in that sign array. After that it's plain substring matching.
Is brute force good enough?+
Probably not. Comparing every window to the pattern costs O(n*m). With n up to 10^5 and a pattern close to that length, it can blow up. Use KMP or the Z-function to get linear time. Brute force is fine only as a correctness check on small inputs.
How hard is this really?+
Easy to medium. The logic is short once you see the sign conversion. The difficulty is knowing KMP cold or recognizing that this is string matching. If you've written a prefix function before, it's about fifteen minutes of work.
What edge cases should I test?+
Check all-equal arrays with a pattern of zeros, strictly decreasing arrays, and a pattern of length n-1 that gives at most one window. Also test negative values and large magnitudes up to 10^9. Subtraction is safe in Python but can overflow in 32-bit languages, so compare instead.
How do I prepare in 48 hours?+
Write KMP from memory twice, once as a prefix function and once as a matcher. Then solve this problem end to end: build the sign array, run the matcher, count hits. Verify against both examples, 4 and 2, before you trust your code.