FastPrepCount Subarrays Matching a Comparison Pattern
Problems/Count Subarrays Matching a Comparison Pattern

Count Subarrays Matching a Comparison Pattern

Capital One logoCapital One● MediumNEW GRADFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report identifies the standard adjacent-comparison pattern task but omits examples, numeric bounds, and a runnable signature. The practice contract supplies those conventional details.

Special thanks: 🌷 A special thank you to a lovely, amazing fren who shared that this question appeared again on October 15, 2026! (∩˃o˂∩)♡ 🩷

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.

More Capital One problems

  • Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
  • Rank Football TeamsOA · Seen Oct 2026
  • Arithmetic Triplets After QueriesOA · Seen Oct 2026
All Capital One interview questionsSee Capital One hiring insights
Saved
public int countMatchingSubarrays(int[] numbers, int[] pattern) {
  // write your code here
}
Checking account…