DIY: Sliding Window Maximum
Solve the interview question "Sliding Window Maximum" in this lesson.
We'll cover the following
Problem statement
For this coding exercise, you are given an array of integers. There is a sub-array (window slide) of size k
, which moves from the extreme left to the extreme right of the given array. The sub-array size remains the same in the program’s complete execution, which is five in this problem. Each time, the sub-array shifts to the right by one position. Your job is to find the list of maximum values in each sub-array.
Input
The inputs consist of an array of integers (array
), the array’s length (size
), and window slide length (k
). The first sub-array starts at index zero of the given array.
array = [12,3,9,15,11,8,2,21,16,5]
size = 10
k = 5
Output
The output is an array that contains a maximum value for each window slide.
[15,15,21,21,21]
Coding exercise
Implement the winSlideMax(array, size, k)
function, where array
is an integer array, size
is the array’s length, and k
is the size of the sub-array. The function will return the array that contains a maximum value for each sub-array.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.