Reported September 2026
Capital Onestring

Count Subarrays Matching a Comparison Pattern

Reported by candidates from Capital One's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Capital One OA. Under 2s to a working solution.
Founder's read

The pattern array only has -1, 0 and 1, and that tiny detail is the whole problem. Capital One reported this OA in September 2026, and it looks like a sliding window question until you spot what it really is: string matching in disguise. You get numbers and a pattern, and you count windows of length pattern.length + 1 whose adjacent comparisons match exactly. With n up to 10^5, brute force will choke. If you blank on the right move, StealthCoder runs invisibly during the live OA and can hand you the approach as a safety net.

The problem

You are given an integer array numbers and a pattern containing only -1, 0, and 1. Each pattern value describes one adjacent comparison: 1 means the next number is greater, 0 means equal, and -1 means 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
Each 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, remain 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 comparison array of length n-1, where each entry is the sign of numbers[i+1] - numbers[i]. Now the task is counting occurrences of pattern inside that array. That's exact substring matching, so use KMP or the Z-function for O(n + m). The naive check of every window costs O(n*m), which times out at 10^5 for large patterns. Common pitfalls: comparing numbers directly instead of signs, off-by-one on window length (it's m + 1 numbers, m comparisons), and forgetting overlapping matches count. Example 1 shows this, since the windows overlap and all four count. Build the failure table on the pattern, scan the sign array, and increment on each full match, then fall back using the table. If KMP details slip under pressure, StealthCoder is the hedge during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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.

⏵ The honest play

You've seen the question. Make sure you actually pass Capital One's OA.

Capital One 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 in the Capital One matching subarrays problem?+

Turn the numbers into a sign array of adjacent comparisons (-1, 0, 1). Then the problem becomes counting how many times the pattern appears as a contiguous substring in that array. Once you see that, it's standard string matching, not a window comparison problem.

Do I need KMP or is brute force enough?+

With numbers.length up to 10^5, brute force is O(n*m) and can hit around 10^10 operations in the worst case. Use KMP or the Z-function for linear time. Rolling hash works too, but KMP avoids collision worries.

How long should the window be?+

A pattern of length m describes m adjacent comparisons, so each candidate window has m + 1 numbers. In the sign array, you're matching m consecutive entries. Mixing these up is the most common off-by-one bug.

Do overlapping matches count?+

Yes. Example 1 has pattern [1,1] on a strictly increasing array of six numbers and returns 4, so overlapping windows each count. In KMP, after a full match, fall back using the failure table instead of resetting to zero.

How do I prepare for this in 48 hours?+

Write KMP from scratch twice: build the prefix table, then run the scan. Test with the two given examples and edge cases like all-equal arrays and a pattern of length 1. Practice converting a problem into a sign or difference array first.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Capital One.

OA at Capital One?
Invisible during screen share
Get it