Count strictly increasing windows of length k in an array of transaction amounts

Quick Overview

Given an array of transaction amounts and a window length k, count how many contiguous subarrays of exactly that length are strictly increasing. Tests sliding window reasoning, strict comparison when neighbors are equal, and reaching linear time on arrays of up to 100,000 values.

Count strictly increasing windows of length k in an array of transaction amounts

Company: Goldman Sachs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array `transactions` of transaction amounts and an integer `k`, count the number of contiguous subarrays of length exactly `k` that are strictly increasing. A subarray is strictly increasing if every element is strictly greater than the element before it. Return the total count of such subarrays. ### Function Signature ```python def count_increasing_segments(transactions: list[int], k: int) -> int: ... ``` ### Rules - Subarrays are counted by position. Two windows that start at different indices count separately, even when they hold the same values. - Equal neighbors are not increasing, so a window that contains two equal adjacent values does not count. - A single element is strictly increasing on its own, so for `k = 1` the answer is `len(transactions)`. ### Constraints - `1 <= len(transactions) <= 10^5` - `1 <= k <= len(transactions)` - `-10^9 <= transactions[i] <= 10^9` ### Examples ```text Input: transactions = [1, 2, 3, 4, 5], k = 3 Output: 3 ``` The strictly increasing subarrays of length 3 are `[1, 2, 3]`, `[2, 3, 4]` and `[3, 4, 5]`, giving a count of 3. ```text Input: transactions = [5, 4, 3, 2, 1], k = 2 Output: 0 ``` No pair of consecutive elements is strictly increasing, so the count is 0. ```text Input: transactions = [1, 3, 3, 4, 5, 6], k = 3 Output: 2 ``` There are four windows of length 3. `[1, 3, 3]` and `[3, 3, 4]` contain the equal pair 3, 3 and fail, while `[3, 4, 5]` and `[4, 5, 6]` are strictly increasing.

Overview: Given an array of transaction amounts and a window length k, count how many contiguous subarrays of exactly that length are strictly increasing. Tests sliding window reasoning, strict comparison when neighbors are equal, and reaching linear time on arrays of up to 100,000 values.

Goldman Sachs logo
Goldman Sachs
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array transactions of transaction amounts and an integer k, count the number of contiguous subarrays of length exactly k that are strictly increasing.

A subarray is strictly increasing if every element is strictly greater than the element before it. Return the total count of such subarrays.

Function Signature

def count_increasing_segments(transactions: list[int], k: int) -> int:
    ...

Rules

  • Subarrays are counted by position. Two windows that start at different indices count separately, even when they hold the same values.
  • Equal neighbors are not increasing, so a window that contains two equal adjacent values does not count.
  • A single element is strictly increasing on its own, so for k = 1 the answer is len(transactions) .

Constraints

  • 1 <= len(transactions) <= 10^5
  • 1 <= k <= len(transactions)
  • -10^9 <= transactions[i] <= 10^9

Examples

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

The strictly increasing subarrays of length 3 are [1, 2, 3], [2, 3, 4] and [3, 4, 5], giving a count of 3.

Input: transactions = [5, 4, 3, 2, 1], k = 2
Output: 0

No pair of consecutive elements is strictly increasing, so the count is 0.

Input: transactions = [1, 3, 3, 4, 5, 6], k = 3
Output: 2

There are four windows of length 3. [1, 3, 3] and [3, 3, 4] contain the equal pair 3, 3 and fail, while [3, 4, 5] and [4, 5, 6] are strictly increasing.