Minimum Size Subarray Sum
Updated
Problem statement
Given an array of positive integers nums and a positive integer target, return the minimum length of a contiguous subarray whose sum is greater than or equal to target. If there is no such subarray, return 0.
Function
minSubArrayLen(target: int, nums: int[]) → intExamples
Example 1
target = 9nums = [2,4,1,5,3,1]return = 3No two neighbours add up to 9: the pairs give 6, 5, 6, 8 and 4. Three numbers are enough, for example [1, 5, 3] with sum 9, so the answer is 3.
Example 2
target = 20nums = [3,1,4]return = 0All three numbers add up to only 8, which is less than 20, so no subarray works and the answer is 0.
Example 3
target = 4nums = [1,4,4]return = 1The single number 4 already reaches the target, so the answer is 1.
Constraints
1 <= target <= 10^91 <= nums.length <= 10^41 <= nums[i] <= 10^4
More Microsoft Online Assessment questions
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
- Maximum Sum of a K-StarOA · Seen Oct 2026
- Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Longest Run of Ones After Flipping k ZerosC3.ai · PHONE SCREENC3.ai interview questions
- Profit AnalysisVirtu Financial · OAVirtu Financial interview questions
- Choose ContainersIMC Trading · OAIMC Trading interview questions
- Calculate Total Distance TravelledAmazon · OA
- TikTok Metrics AnalysisTikTok · OATikTok interview questions
- Load BalancingMathWorks · OAMathWorks interview questions
- Maximum Distinct Items Within a BudgetAdobe · OAAdobe interview questions
- Size-Weighted Random Server PicksDocuSign · OADocuSign interview questions
- Calculating QuantilesIndeed · OAIndeed interview questions
- Count Lamps Lighting Each PointCapital One · OACapital One interview questions