Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Minimum Size Subarray Sum
mediumsliding-window
Problem
You are given an array of positive integers nums and a positive integer target. Find the length of the shortest contiguous subarray whose elements sum to at least target. If no subarray reaches the target, return 0.
numshas between 1 and 100,000 elements, each a positive integer no larger than 10,000.targetis a positive integer up to 10^9, so the sum of the entire array may fall short of it.
Examples
nums = [4, 1, 6, 2, 8, 3], target = 11→2.[8, 3]reaches 11 in two elements; no single element does.nums = [2, 9, 5], target = 9→1. The9on its own meets the target.nums = [3, 3, 3, 3], target = 20→0. The whole array only sums to 12.
Hints
- All numbers are positive. What happens to a subarray's sum as you extend it, and as you trim it?
- Once a window's sum reaches the target, extending it further cannot make it shorter, so what should you do instead?
- Sliding window: grow the right end until the sum is at least
target, then shrink from the left while it still is, recording the length at each shrink step.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.