Skip to content
CodeKruPro

Maximum of every window of size k

Coding problemHardAsked in: Adobe, Amazon, Apple, Google, Meta, Microsoft

Given a list of integers nums and a whole number k, slide a window of k consecutive values from the left end of the list to the right end, one position at a time. Return the largest value in each window, in order.

A list of n values has n - k + 1 windows.

Examples

Example 1
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Example 2
Input: nums = [1], k = 1
Output: [1]
Example 3
Input: nums = [9,8,7], k = 2
Output: [9,8]

Constraints

  • 1 <= k <= n <= 300,000, where n is the number of values
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in