Return the maximum of every sliding window of size k

Quick Overview

Return the largest value in every window of k consecutive elements as the window slides left to right across an integer array of up to 100,000 values. It tests whether you can beat the O(nk) brute force while keeping window boundaries, negative values and repeated maximums correct.

Return the maximum of every sliding window of size k

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an array of integers `nums` and a window size `k`, a window of exactly `k` consecutive elements starts at the left end of the array and moves right one position at a time until it reaches the right end. Return the maximum element of each window, in the order the windows appear. ### Function Signature ```python def max_sliding_window(nums: list[int], k: int) -> list[int]: ``` ### Rules - The window starting at index `i` covers `nums[i]` through `nums[i + k - 1]`. There are `len(nums) - k + 1` windows, so the returned list has that length. - The output lists the maximums from the leftmost window to the rightmost one. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^4 <= nums[i] <= 10^4` - `1 <= k <= len(nums)` ### Examples #### Example 1 ```text Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7] ``` The six windows are `[1,3,-1]`, `[3,-1,-3]`, `[-1,-3,5]`, `[-3,5,3]`, `[5,3,6]` and `[3,6,7]`, and their maximums are 3, 3, 5, 5, 6 and 7. #### Example 2 ```text Input: nums = [1,-1,-2,3,4,2], k = 2 Output: [1,-1,3,4,4] ``` The windows are `[1,-1]`, `[-1,-2]`, `[-2,3]`, `[3,4]` and `[4,2]`. The second window holds only negative numbers, so its maximum is -1. #### Example 3 ```text Input: nums = [9,7,9,1,1], k = 4 Output: [9,9] ``` The windows are `[9,7,9,1]` and `[7,9,1,1]`. The first 9 leaves after the first window, but the second 9 at index 2 is still inside, so the second maximum is also 9.

Overview: Return the largest value in every window of k consecutive elements as the window slides left to right across an integer array of up to 100,000 values. It tests whether you can beat the O(nk) brute force while keeping window boundaries, negative values and repeated maximums correct.

Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums and a window size k, a window of exactly k consecutive elements starts at the left end of the array and moves right one position at a time until it reaches the right end. Return the maximum element of each window, in the order the windows appear.

Function Signature

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

Rules

  • The window starting at index i covers nums[i] through nums[i + k - 1] . There are len(nums) - k + 1 windows, so the returned list has that length.
  • The output lists the maximums from the leftmost window to the rightmost one.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= len(nums)

Examples

Example 1

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]

The six windows are [1,3,-1], [3,-1,-3], [-1,-3,5], [-3,5,3], [5,3,6] and [3,6,7], and their maximums are 3, 3, 5, 5, 6 and 7.

Example 2

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

The windows are [1,-1], [-1,-2], [-2,3], [3,4] and [4,2]. The second window holds only negative numbers, so its maximum is -1.

Example 3

Input: nums = [9,7,9,1,1], k = 4
Output: [9,9]

The windows are [9,7,9,1] and [7,9,1,1]. The first 9 leaves after the first window, but the second 9 at index 2 is still inside, so the second maximum is also 9.