Back to Practice Dashboard
Top 150 InterviewHard

Sliding Window Maximum

Learn how to solve the 'Sliding Window Maximum' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Hard

You are given an array of integers nums and an integer k representing the size of a sliding window which moves from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max value in each sliding window.

Write a function maxSlidingWindow(nums: List[int], k: int) -> List[int].

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]
Explanation

Window [1,3,-1] max=3, [3,-1,-3] max=3, [-1,-3,5] max=5, [-3,5,3] max=5, [5,3,6] max=6, [3,6,7] max=7.

Example 2
Input
nums = [1], k = 1
Output
[1]
Explanation

Single element window, max is 1.

Need a Hint?
Maintain a sliding window boundary using two pointers. Expand the right boundary, and contract the left boundary if criteria are violated.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor