The article discusses the sliding window pattern as an efficient approach to find the smallest subarray with a sum greater than or equal to a given target, a common problem in coding interviews.
Abstract
The article "Smallest Subarray with a Given Sum | Coding Interview | Sliding-Window Pattern" by Ganesh Prasad provides an in-depth guide to solving a specific coding problem that is often asked in interviews at tech companies like Goldman Sachs, Meta, Amazon, and Google. The problem involves finding the smallest subarray within an array of integers that has a sum equal to or exceeding a specified target value. The author explains two approaches to solve this problem: a brute force method with a cubic time complexity of O(n³) and a more efficient sliding window approach with a linear time complexity of O(n). The brute force method involves checking every possible subarray, while the sliding window approach dynamically adjusts the size of the window to find the smallest subarray that meets the criteria. The article also includes examples, illustrations, and code snippets in C++ to help readers understand the concepts and implement the solution. The author emphasizes the importance of understanding when to use the sliding window pattern and provides a link to a 30-day interview preparation plan for further practice.
Opinions
The author believes that the sliding window pattern is the best starting point for mastering problems based on this pattern.
The article suggests that the sliding window approach is superior to the brute force method due to its linear time complexity, making it more suitable for coding interviews.
The author values the importance of preprocessing the list to improve efficiency, even suggesting an optimization to reduce the brute force method's complexity from O(n³) to O(n²) by using a prefix sum array.
Ganesh Prasad encourages readers to support Medium writers by signing up for a membership, indicating a community-oriented perspective.
The author's use of illustrative GIFs, images, and well-commented code snippets conveys a commitment to making complex concepts accessible and understandable to a broad audience.
The article concludes with a call to action for readers to clap for the article if they find it helpful, and to follow the author for updates on future stories.
Smallest Subarray with a Given Sum | Coding Interview | Sliding-Window Pattern
If you are planning to perfect your skill to solve problems based on the sliding window pattern, In my opinion, this is the best starting point. It is also important as these tech companies (Goldman Sachs, Meta, Amazon, and Google) have asked this question.
The important thing to get out of this article is when we should use the sliding window pattern. We have already discussed a similar problem, Longest Repeating string after K replacements.
This article belongs to the interview preparation plan, which you can find here.
You are given an array of integers A and an integer target, and you have to find the smallest Subarray of A such that the sum of its elements is greater or equal to the target.
Explanation: The smallest subarray with sum greater orequalto8is [2, 6].
Example 2:
Input: [2, 2, 2, 2] and target = 10Output:0
Explanation: No subarray exists whose sum is greater orequalto10.
Brute Force Approach
We can naively go through each Subarray, n being the size of the array, we will have n² subarrays. For each Subarray, we will find the sum of its elements and remember the size of the smallest Subarray with a total greater than or equal to our target.
The Naive Algorithm
Use two for loops to traverse every Subarray, where the outer loop will point to the start, and the inner loop will point to the end of the subarrays.
Use another loop, to sum up the elements of the current Subarray.
If the sum is greater or equal to the target and the size of the current Subarray is smaller than our minimum size so far, then update the minimum size with the current subarray size.
The naive algorithm, where we look at every Subarray possible and find the length of the minimum Subarray with a sum greater or equal to the target.
Here the time complexity will be cubic, O(n³) in nature because we are using three nested loops, the outer two for loops are for traversing the subarrays, and the innermost loop is used to get the sum of the current Subarray.
While we are at it, let's look at its code. I have commented on the code well, so if c++ is not your primary language, you can still understand what is happening.
Code
Output:
The lengthofthe smallest subarray with target 8is2
Of course, we can improve it from O(n³) to O(n²); if we preprocess the list and get the prefix sum array (O(n)), then getting the sum of any subarray will be a constant operation (the following figure).
Find the prefix sum array of A, and from there, we can get the sum of any subarray in constant time.
Now let's see some magic and understand an easy solution with linear complexity.
The strategy to solve this problem is similar to the Maximum Sum Subarray of Size K and in this approach, as the name suggests, we slide a window from left to right and change the window's size suiting our needs. A window on an array can be controlled by using two variables, one pointing to the start and the other pointing to the end.
Here a window on the array is controlled by the two variables "start" and "end."
For this problem, our window will extend from the right side by adding new elements as long as the sum of its elements is less than the target. And since we need the smallest Subarray, once the sum of the window is greater than or equal to the target, we will start shrinking the window from the left.
At any point, if a window has a sum greater than or equal to the target and its size is smaller than what we have seen so far, we will remember this new size.
This process will end once the last element of the array has been added to the sliding window.
Let's see it using an example.
Its code is as simple as the solution itself. It's well commented (to make it language independent) and written in c++.
Code
Output:
The lengthofthe smallest subarray with target 8is2
Time & Space complexity
Time: We can see that the outer for loop add each element only once, and the inner while removes (if it does) only once hence the given approach is linear, O(n) in nature.
Space: We are not using space for this algorithm, so its space complexity is constant, O(1).
Thanks for reading.
I hope this article was helpful to you.
If you liked what you just read, a clap 🤗🤗🤗would be highly appreciated. You can be generous in clapping; it shows me how much you enjoyed this story. And if you didn't like it? Please do comment😋!
P.S.: If you like this uninterrupted reading experience on this beautiful platform of Medium.com, consider supporting the writers of this community by signing up for a membership HERE. It only costs $5 per month and supports all the writers.
You can also follow me HERE to get updates on the stories that I write.