Quick Overview

A coding task that asks for the number of contiguous windows of length k in an integer array whose elements are strictly increasing. With arrays of up to 200,000 elements, it tests whether a candidate can avoid re-checking every window and count all qualifying windows in time linear in the array length.

Count Length-k Windows That Are Strictly Increasing

Company: Confluent

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

Given an integer array `nums` and an integer `k`, count the contiguous windows of exactly `k` elements whose values are strictly increasing from left to right. Windows may overlap, and each starting position whose window qualifies is counted once. The candidate's report describes the test only as a monotonic condition on each window; this version fixes it as strict increase, defined exactly in Rules below. ### Function Signature ```python def count_increasing_windows(nums: list[int], k: int) -> int: ``` ### Rules - The windows are the slices `nums[i : i + k]` for every starting index `i` with `0 <= i <= len(nums) - k`, so there are `len(nums) - k + 1` of them. - A window qualifies when each of its elements is strictly less than the next element inside the window: `nums[j] < nums[j + 1]` for every `j` with `i <= j < i + k - 1`. Two equal neighbours break strict increase. - A window of length 1 contains no neighbouring pair and always qualifies. - Return the number of qualifying windows as a single integer. ### Constraints - `1 <= k <= len(nums) <= 2 * 10^5` - `-10^9 <= nums[i] <= 10^9` - `len(nums)` and `k` can both reach `2 * 10^5`, so checking each window from scratch exceeds the time limit; the expected solution runs in time linear in `len(nums)`. ### Examples **Example 1** ```text Input: nums = [1, 2, 3, 2, 4, 5, 6], k = 3 Output: 3 ``` The five windows are `[1, 2, 3]`, `[2, 3, 2]`, `[3, 2, 4]`, `[2, 4, 5]` and `[4, 5, 6]`. The first, fourth and fifth are strictly increasing. **Example 2** ```text Input: nums = [5, 5, 6, 7], k = 2 Output: 2 ``` `[5, 5]` does not qualify because its two values are equal; `[5, 6]` and `[6, 7]` do. **Example 3** ```text Input: nums = [4, -1, 3], k = 1 Output: 3 ``` Every window has length 1, so all three qualify.

Overview: A coding task that asks for the number of contiguous windows of length k in an integer array whose elements are strictly increasing. With arrays of up to 200,000 elements, it tests whether a candidate can avoid re-checking every window and count all qualifying windows in time linear in the array length.

Read the full Confluent Software Engineer interview experience this question came from

Given an integer array `nums` and an integer `k`, count the contiguous windows of exactly `k` elements whose values are strictly increasing from left to right. Windows may overlap, and each starting position whose window qualifies is counted once. ### Rules - The windows are the slices `nums[i : i + k]` for every starting index `i` with `0 <= i <= len(nums) - k`, so there are `len(nums) - k + 1` of them. - A window qualifies when each of its elements is strictly less than the next element inside the window: `nums[j] < nums[j + 1]` for every `j` with `i <= j < i + k - 1`. Two equal neighbours break strict increase. - A window of length 1 contains no neighbouring pair and always qualifies. - Return the number of qualifying windows as a single integer. ### Example 1 ```text Input: nums = [1, 2, 3, 2, 4, 5, 6], k = 3 Output: 3 ``` The five windows are `[1, 2, 3]`, `[2, 3, 2]`, `[3, 2, 4]`, `[2, 4, 5]` and `[4, 5, 6]`. The first, fourth and fifth are strictly increasing. ### Example 2 ```text Input: nums = [5, 5, 6, 7], k = 2 Output: 2 ``` `[5, 5]` does not qualify because its two values are equal; `[5, 6]` and `[6, 7]` do. ### Constraints - `1 <= k <= len(nums) <= 2 * 10^5` - `-10^9 <= nums[i] <= 10^9` - `len(nums)` and `k` can both reach `2 * 10^5`, so checking each window from scratch is too slow; the expected solution runs in time linear in `len(nums)`. - No value exceeds 2^31 - 1: every element and the answer (at most `len(nums) - k + 1`) fit in a signed 32-bit integer.

Constraints

  • 1 <= k <= len(nums) <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9
  • len(nums) and k can both reach 2 * 10^5, so the expected solution runs in time linear in len(nums)
  • Every element and the answer (at most len(nums) - k + 1) fit in a signed 32-bit integer

Examples

Input: ([7], 1)

Expected Output: 1

Explanation: Minimum valid input: one element with k = 1 forms one window, which always qualifies.

Input: ([4, -1, 3], 1)

Expected Output: 3

Explanation: Source example 3: with k = 1 every one of the three windows qualifies.

Hints

  1. A window of k elements qualifies exactly when all k - 1 neighbouring pairs inside it are strictly increasing; an equal pair counts as a break.
  2. Neighbouring windows share all but one element, so information about one window can be reused for the next instead of rechecking every pair.
  3. With k = 1 every window qualifies, so the answer is simply len(nums).

Loading coding console...

Show the approach

Approach

Scan the array once while tracking run, the length of the longest strictly increasing contiguous segment that ends at the current index j. If j > 0 and nums[j - 1] < nums[j], the segment ending at j - 1 extends by one element, so run grows by one; otherwise (a drop or an equal neighbour) run resets to 1. Identify each window by its last index j: the window nums[j - k + 1 : j + 1] qualifies exactly when all k - 1 of its neighbouring pairs are strictly increasing, which holds exactly when run >= k at index j. Because run never exceeds j + 1, run >= k also implies j >= k - 1, so every counted index ends a real window and each qualifying window is counted exactly once. Edge cases: k = 1 makes every index count, so the answer is len(nums); k = len(nums) leaves a single window, counted only when the whole array is strictly increasing; equal neighbours reset the run because the comparison is strict; values are only compared, never added, so the +/-10^9 bounds cause no overflow.

Time complexity:
O(n)
Space complexity:
O(1)