You have 2 free member-only stories left this month.
Coding Interview Made Easy: Understanding the Sliding Window Pattern
In many problems dealing with an array (or a LinkedList), we are asked to find or calculate something among all the contiguous subarrays (or sublists) of a given size. For example, take a look at this problem:
Given an array, find the average of all contiguous subarrays of size ‘K’ in it.
Let’s understand this problem with a real input:
Array: [1, 3, 2, 6, -1, 4, 1, 8, 2], K=5
Here, we are asked to find the average of all contiguous subarrays of size ‘5’ in the given array. Let’s solve this:
- For the first 5 numbers (subarray from index 0–4), the average is: (1+3+2+6–1)/5 => 2.2
- The average of next 5 numbers (subarray from index 1–5) is: (3+2+6–1+4)/5 => 2.8
- For the next 5 numbers (subarray from index 2–6), the average is: (2+6–1+4+1)/5 => 2.4
Here is the final output containing the averages of all contiguous subarrays of size 5:
Output: [2.2, 2.8, 2.4, 3.6, 2.8]
Brute-force algorithm
A brute-force algorithm will calculate the sum of every 5-element contiguous subarray of the given array and divide the sum by ‘5’ to find the average. This is what the algorithm will look like in Python3:
Time complexity: Since for every element of the input array, we are calculating the sum of its next ‘K’ elements, the time complexity of the above algorithm will be O(N∗K) where ’N’ is the number of elements in the input array.
Can we find a better solution? Do you see any inefficiency in the above approach?
Sliding Window to the rescue
The inefficiency is that for any two consecutive subarrays of size ‘5’, the overlapping part (which will contain four elements) will be evaluated twice. For example, take the above-mentioned input:
As you can see, there are four overlapping elements between the subarray (indexed from 0–4) and the subarray (indexed from 1–5). Can we somehow reuse the sum
we have calculated for the overlapping elements?
The efficient way to solve this problem would be to visualize each subarray as a sliding window of ‘5’ elements. This means that we will slide the window by one element when we move on to the next subarray. To reuse the sum
from the previous subarray, we will subtract the element going out of the window and add the element now being included in the sliding window. This will save us from going through the whole subarray to find the sum
and, as a result, the algorithm complexity will reduce to O(N).
Here is the algorithm for the Sliding Window approach:
In some problems, the size of the sliding window is not fixed. We have to expand or shrink the window based on the problem constraints.
Here are a few more problems that can be solved using the Sliding Window approach:
- Maximum Sum Subarray of Size K (easy)
- Smallest Subarray with a given sum (easy)
- Fruits into Baskets (medium)
- Longest Substring with K Distinct Characters (medium)
- Longest Substring with Distinct Characters (hard)
- Permutation in a String (hard)
- String Anagrams (hard)
- Words Concatenation (hard)
Learn more about Sliding Window patter in Grokking the Coding Interview.
Here are a few good posts on coding and system design interviews:
Thanks for reading
- 👏 Please clap for the story and follow me 👉
- 📰 View more content on Coding and System Design Interviews
- 🔔 Follow me: LinkedIn | Twitter | Newsletter