Skip to content
HardSliding WindowAI interview only

Sliding Window Maximum

Asked atgoogleamazonmetamicrosoftbloomberguberapple

01 · Problem

You are given an array of integers nums and an integer k representing the size of a sliding window. The window moves from the very left of the array to the very right, one position at a time. Return an array of the maximum value in each window position.

02 · Examples

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

As the window slides from left to right, the maximum in each window of size 3 is [3,3,5,5,6,7].

Example 02
Input
nums = [1], k = 1
Output
[1]

The only window contains the single element 1.

Example 03
Input
nums = [9,11], k = 2
Output
[11]

The single window [9,11] has maximum 11.

03 · Constraints

  • 011 <= nums.length <= 105
  • 02-104 <= nums[i] <= 104
  • 031 <= k <= nums.length

04 · Optimal complexity

Time
O(n)
Space
O(k)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.