‹ DS&A interview · Socratic
DSA · Sliding Window · #125

Minimum Size Subarray Sum

Module 71 · difficulty 3/5·30:00starts on first keystroke

Given an array of positive integers `nums` and a positive integer `target`, return the **minimal length** of a contiguous subarray whose sum is **greater than or equal to** `target`. If there is no such subarray, return `0`. Implement `minSubArrayLen(target, nums)` which takes the integer `target` and the array `nums`, and returns the minimal subarray length as an integer.

Examples
  • target = 7, nums = [2,3,1,2,4,3] 2The subarray [4,3] has sum 7 >= 7 and length 2, which is minimal.
  • target = 4, nums = [1,4,4] 1The single element [4] already meets the target.
  • target = 11, nums = [1,1,1,1,1,1,1,1] 0The total sum is 8 < 11, so no valid subarray exists.
Constraints
  • · 1 <= target <= 10^9
  • · 1 <= nums.length <= 10^5
  • · 1 <= nums[i] <= 10^4
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.