Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sliding Window Maximum with Deque

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Sliding Window Maximum with Deque. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

NVIDIA Nsight Systems can produce a time-ordered stream of kernel durations or telemetry values. Given an integer array nums and a window size k, return the maximum value in every contiguous window as the window moves from left to right by one position.

Implement the solution in O(n) time using a deque. The deque should store indices, with corresponding values maintained in decreasing order.

Formal Specification

  • Input: nums, a non-empty list of integers, and k, an integer window size.
  • Output: A list containing n - k + 1 integers, where each integer is the maximum value in one window.
  • The first output corresponds to nums[0:k], and the final output corresponds to nums[n-k:n].

Constraints

  • 1 <= len(nums) <= 100000
  • 1 <= k <= len(nums)
  • -10^9 <= nums[i] <= 10^9
  • The input array is non-empty

Function Signature

def max_window(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output