Coding interview problems โบ Sliding Window
๐ช Sliding Window Medium
Minimum Size Subarray Sum
Minimum Size Subarray Sum explained: a variable sliding window grows until the sum reaches the target, then shrinks from the left in O(n). Tested Python.
Also searched as: smallest subarray with sum at least target ยท variable size sliding window
Try it live Free
Sliding Window
Watch the window add what enters and subtract what leaves. In this problem the window size changes: it stretches right until the sum is enough, then shrinks from the left.
The problem
Given a list of positive integers and a positive target, return the length of the shortest contiguous subarray whose sum is at least target, or 0 if none exists.
[4, 3].
The key insight
Since every number is positive, growing the window always increases its sum and shrinking always decreases it. So extend the right edge until the sum is big enough, then pull the left edge in as far as possible while the sum stays big enough, recording the length each time.
Step-by-step approach
- Set
left = 0,s = 0,best = infinity. - For each
right, addnums[right]tos. - While
s >= target: recordright - left + 1, subtractnums[left], moveleftright. - Return
best, or 0 if it was never set.
Python solution
def min_subarray_len(target, nums):
left = s = 0
best = float("inf")
for right, x in enumerate(nums):
s += x
while s >= target: # shrink while still valid
best = min(best, right - left + 1)
s -= nums[left]
left += 1
return 0 if best == float("inf") else bestComplexity
Each index enters the window once and leaves at most once, so the nested while loop does O(n) total work.
Common mistakes
- Using
ifinstead ofwhileto shrink, which leaves windows longer than necessary. - Applying this window to arrays with negative numbers, where shrinking may increase the sum; that needs prefix sums with a deque instead.
- Returning infinity instead of 0 when no subarray reaches the target.
Frequently asked questions
What is the time and space complexity of Minimum Size Subarray Sum?
O(n) time and O(1) space with the variable sliding window. A prefix sums plus binary search approach is O(n log n).
Why must the numbers be positive?
Positivity guarantees that a longer window has a larger sum, so once a window is valid there is no point extending it further from the same left edge. With negatives that guarantee breaks.
Is the nested loop really O(n)?
Yes. left only moves forward and never passes right, so across the whole run the inner loop executes at most n times.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Sliding Window Sliding Window lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.