Check Whether Each Odd-Length Window Strictly Decreases Outward From Its Center

Quick Overview

Given an integer array and an odd window size, return for every window position whether the values strictly decrease from the center element out to both edges. It tests sliding window reasoning, careful handling of strict comparisons, and finding a linear-time check for large inputs.

Check Whether Each Odd-Length Window Strictly Decreases Outward From Its Center

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums` and an odd integer `k`, slide a window of size `k` across the array. For each window position, check if the values strictly decrease from the center point toward both the left and right boundaries of the window. Return a list of boolean values, one for each window position, indicating whether the monotonic decreasing condition from center to both edges is satisfied. More formally, for a window with center index `c`, the condition holds if and only if `nums[c] > nums[c-1] > ... > nums[c-k//2]` and `nums[c] > nums[c+1] > ... > nums[c+k//2]`. ### Function Signature ```python def monotonic_window(nums: list[int], k: int) -> list[bool]: ``` ### Rules - The window that starts at index `i` covers `nums[i]` through `nums[i + k - 1]`, and its center is index `i + k // 2`. - Return exactly `len(nums) - k + 1` booleans, ordered by window start from left to right. - Every comparison is strict. Two adjacent equal values inside the window make the condition false. - When `k = 1` the window holds only its center, so the condition is true at every position. ### Constraints - `1 <= nums.length <= 10^5` - `1 <= k <= nums.length` - `k` is odd - `-10^4 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [1,3,5,4,2,6,7], k = 5 Output: [True, False, False] ``` Only the first window, `[1,3,5,4,2]`, qualifies: 5 > 3 > 1 on the left and 5 > 4 > 2 on the right. The second window's center 4 is smaller than its left neighbor 5, and the third window's center 2 is smaller than its left neighbor 4. ```text Input: nums = [5,4,3,2,1], k = 3 Output: [False, False, False] ``` The array decreases from left to right, so each center (4, then 3, then 2) is smaller than its left neighbor. No window qualifies. ```text Input: nums = [1,3,2,3,3], k = 3 Output: [True, False, False] ``` Window `[1,3,2]` qualifies because 3 > 1 and 3 > 2. Window `[3,2,3]` fails at its center 2, and window `[2,3,3]` fails because 3 > 3 is false on the right.

Overview: Given an integer array and an odd window size, return for every window position whether the values strictly decrease from the center element out to both edges. It tests sliding window reasoning, careful handling of strict comparisons, and finding a linear-time check for large inputs.

Pinterest logo
Pinterest
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums and an odd integer k, slide a window of size k across the array. For each window position, check if the values strictly decrease from the center point toward both the left and right boundaries of the window.

Return a list of boolean values, one for each window position, indicating whether the monotonic decreasing condition from center to both edges is satisfied.

More formally, for a window with center index c, the condition holds if and only if nums[c] > nums[c-1] > ... > nums[c-k//2] and nums[c] > nums[c+1] > ... > nums[c+k//2].

Function Signature

def monotonic_window(nums: list[int], k: int) -> list[bool]:

Rules

  • The window that starts at index i covers nums[i] through nums[i + k - 1] , and its center is index i + k // 2 .
  • Return exactly len(nums) - k + 1 booleans, ordered by window start from left to right.
  • Every comparison is strict. Two adjacent equal values inside the window make the condition false.
  • When k = 1 the window holds only its center, so the condition is true at every position.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • k is odd
  • -10^4 <= nums[i] <= 10^4

Examples

Input: nums = [1,3,5,4,2,6,7], k = 5
Output: [True, False, False]

Only the first window, [1,3,5,4,2], qualifies: 5 > 3 > 1 on the left and 5 > 4 > 2 on the right. The second window's center 4 is smaller than its left neighbor 5, and the third window's center 2 is smaller than its left neighbor 4.

Input: nums = [5,4,3,2,1], k = 3
Output: [False, False, False]

The array decreases from left to right, so each center (4, then 3, then 2) is smaller than its left neighbor. No window qualifies.

Input: nums = [1,3,2,3,3], k = 3
Output: [True, False, False]

Window [1,3,2] qualifies because 3 > 1 and 3 > 2. Window [3,2,3] fails at its center 2, and window [2,3,3] fails because 3 > 3 is false on the right.