Count Windows That Follow an Increase, Equal, Decrease Pattern

Read the full interview experience this question came from →

Quick Overview

A coding question that gives an integer array and a pattern of 1, 0 and -1 values standing for an increase, an equal step and a decrease, and asks how many windows of consecutive elements follow the pattern exactly. It tests translating pairwise comparisons into steps and counting overlapping matches efficiently.

Count Windows That Follow an Increase, Equal, Decrease Pattern

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

You are given an integer array `numbers` and an array `pattern` whose values are each `1`, `0` or `-1`. Let `m = len(pattern)`. A window of `m + 1` consecutive elements, `numbers[s]` through `numbers[s + m]`, follows the pattern when every step inside it has the direction that `pattern` gives for that step: - `pattern[k] == 1` means `numbers[s + k + 1] > numbers[s + k]` - `pattern[k] == 0` means `numbers[s + k + 1] == numbers[s + k]` - `pattern[k] == -1` means `numbers[s + k + 1] < numbers[s + k]` Return the number of starting indices `s` whose window follows the pattern. ### Function Signature ```python def count_pattern_windows(numbers: list[int], pattern: list[int]) -> int: ``` ### Rules - A window must lie entirely inside `numbers`, so `s` ranges from `0` to `len(numbers) - m - 1`. - Every one of the `m` conditions must hold; a single mismatched step disqualifies the window. - Windows may overlap, and each matching starting index is counted once. - Return `0` when no window matches. ### Constraints - `2 <= len(numbers) <= 10^5` - `1 <= len(pattern) < len(numbers)` - `-10^9 <= numbers[i] <= 10^9` - every `pattern[k]` is `1`, `0` or `-1` ### Examples **Example 1** ```text Input: numbers = [3, 7, 5, 5, 8, 7], pattern = [-1, 0, 1] Output: 1 ``` The only matching window starts at index 1: `[7, 5, 5, 8]` steps down from 7 to 5, stays equal from 5 to 5, then steps up from 5 to 8. Put differently, the elements `5, 5, 8` at indices 2 to 4 each compare with the element just before them as the pattern requires. The windows starting at index 0 (`[3, 7, 5, 5]`, whose first step goes up) and index 2 (`[5, 5, 8, 7]`, whose first step is equal) do not match. **Example 2** ```text Input: numbers = [2, 2, 2, 2], pattern = [0, 0] Output: 2 ``` The windows starting at indices 0 and 1 both stay equal twice; overlapping windows are counted separately. **Example 3** ```text Input: numbers = [5, 4, 5, 4, 5], pattern = [1, -1] Output: 1 ``` Only the window starting at index 1, `[4, 5, 4]`, goes up and then down. The windows starting at indices 0 and 2 begin with a decrease.

Overview: A coding question that gives an integer array and a pattern of 1, 0 and -1 values standing for an increase, an equal step and a decrease, and asks how many windows of consecutive elements follow the pattern exactly. It tests translating pairwise comparisons into steps and counting overlapping matches efficiently.

Read the full Capital One Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Capital One
Capital One logo
Capital One
Oct 16, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

You are given an integer array numbers and an array pattern whose values are each 1, 0 or -1. Let m = len(pattern). A window of m + 1 consecutive elements, numbers[s] through numbers[s + m], follows the pattern when every step inside it has the direction that pattern gives for that step:

  • pattern[k] == 1 means numbers[s + k + 1] > numbers[s + k]
  • pattern[k] == 0 means numbers[s + k + 1] == numbers[s + k]
  • pattern[k] == -1 means numbers[s + k + 1] < numbers[s + k]

Return the number of starting indices s whose window follows the pattern.

Function Signature

def count_pattern_windows(numbers: list[int], pattern: list[int]) -> int:

Rules

  • A window must lie entirely inside numbers , so s ranges from 0 to len(numbers) - m - 1 .
  • Every one of the m conditions must hold; a single mismatched step disqualifies the window.
  • Windows may overlap, and each matching starting index is counted once.
  • Return 0 when no window matches.

Constraints

  • 2 <= len(numbers) <= 10^5
  • 1 <= len(pattern) < len(numbers)
  • -10^9 <= numbers[i] <= 10^9
  • every pattern[k] is 1 , 0 or -1

Examples

Example 1

Input:  numbers = [3, 7, 5, 5, 8, 7], pattern = [-1, 0, 1]
Output: 1

The only matching window starts at index 1: [7, 5, 5, 8] steps down from 7 to 5, stays equal from 5 to 5, then steps up from 5 to 8. Put differently, the elements 5, 5, 8 at indices 2 to 4 each compare with the element just before them as the pattern requires. The windows starting at index 0 ([3, 7, 5, 5], whose first step goes up) and index 2 ([5, 5, 8, 7], whose first step is equal) do not match.

Example 2

Input:  numbers = [2, 2, 2, 2], pattern = [0, 0]
Output: 2

The windows starting at indices 0 and 1 both stay equal twice; overlapping windows are counted separately.

Example 3

Input:  numbers = [5, 4, 5, 4, 5], pattern = [1, -1]
Output: 1

Only the window starting at index 1, [4, 5, 4], goes up and then down. The windows starting at indices 0 and 2 begin with a decrease.