Reported September 2026
TikTokarray

Count Subarrays Matching a Comparison Pattern

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

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

Input size is the whole story here. TikTok reported this one in September 2026, and with numbers up to 10^5 long, checking every window against the pattern the obvious way can drift toward quadratic. You're given an array and a pattern of -1, 0 and 1, and you count windows whose adjacent comparisons match exactly. It's string matching wearing an array costume. If you've got an OA coming up, learn the conversion trick below. StealthCoder sits invisibly as a safety net on the live OA if your mind goes blank mid-problem.

The problem

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-sign array of length n-1, where each entry is 1, 0 or -1 based on comparing neighbors. Now the question is how many times pattern appears as a contiguous substring of that array. Brute force costs O(n*m), which can hit around 2.5*10^9 operations when m is near n/2. Use KMP or the Z-function to get O(n+m). Build the failure table on the pattern, scan the sign array, and count each full match. Common pitfalls: off-by-one on window length (pattern.length + 1 elements, so n - m windows), using subtraction that overflows in languages with 32-bit ints, and forgetting to keep overlapping matches. Example 1 has overlapping matches, so don't skip ahead after a hit. If you freeze on KMP during the live OA, StealthCoder is the hedge that gives you the working version.

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 ii. If you have time before the OA, drill that.

⏵ The honest play

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

TikTok 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 for Count Subarrays Matching a Comparison Pattern?+

Turn numbers into an array of signs by comparing each adjacent pair, giving 1, 0 or -1. Then the problem is just counting occurrences of pattern inside that sign array. Once you see it as substring matching, the rest is a standard linear-time algorithm.

Will brute force pass with 10^5 elements?+

Probably not reliably. Brute force checks up to n-m windows with m comparisons each, which is quadratic in the worst case. With n at 10^5 and a long pattern, that's billions of operations. Use KMP or Z-function for O(n+m) so you don't gamble on test data.

Do overlapping matches count?+

Yes. Example 1 shows it: [1,1] matches at four overlapping positions in a strictly increasing array of six. Every starting index is its own subarray. With KMP, after a full match you fall back using the failure table instead of jumping past the match.

How do I compute the sign without bugs?+

Compare directly: if b > a it's 1, if b == a it's 0, otherwise -1. Avoid subtracting values, since the range goes to plus or minus 10^9 and the difference can overflow a 32-bit integer in some languages. Direct comparison is safe and clear.

How should I prepare for this in 48 hours?+

Write KMP's prefix function from scratch twice, then solve this problem using it on the sign array. Test with all zeros, a pattern as long as n-1, and the overlapping case from Example 1. That covers the common failures for this pattern on a TikTok-style OA.

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

OA at TikTok?
Invisible during screen share
Get it