Shortest Subarray with an Exact Target Sum
Updated
Problem statement
Given an integer array nums and a target, return the shortest length of a nonempty consecutive part of the array whose sum is exactly target. Return 0 when no such subarray exists.
For this exercise, assume values may be negative or zero. A sum greater than the target does not qualify.
Function
shortestExactSum(nums: int[], target: long) → intExamples
Example 1
nums = [2,-1,2,1]target = 3return = 2The final two values [2,1] sum to 3. No single value equals 3.
Example 2
nums = [2,2]target = 3return = 0The available nonempty subarray sums are 2 and 4; neither equals 3.
Constraints
- For this exercise, assume
0 <= nums.length <= 10^5. -10^9 <= nums[i] <= 10^9.-10^14 <= target <= 10^14; use 64-bit prefix sums.
More PeopleStrong Phone Screen questions
- Reverse Linked-List Nodes in Groups of KOA · PHONE SCREEN · Seen Mar 2026
- Level of the Minimum-Sum SubtreePHONE SCREEN · Seen Jan 2026
- Ceiling Value in a Sorted ArrayPHONE SCREEN · Seen Sep 2025
- Maximum Employee Salary for a CompanyPHONE SCREEN · Seen Sep 2025
- Valid ParenthesesPHONE SCREEN · Seen Sep 2025
- Number of ProvincesPHONE SCREEN · Seen May 2025
- Add Forward Linked-List Numbers Using RecursionPHONE SCREEN · Seen Oct 2021
- Running Median of a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2021
- Build an Expression Tree from PostfixPHONE SCREEN · Seen Oct 2021
- Maximum Score from Deleting Neighboring ValuesPHONE SCREEN · Seen Oct 2021
Similar questions at other companies
- Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
- Equal Sum Split After One RemovalGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Maximum Size Subarray Sum Equals KeBay · PHONE SCREENeBay interview questions
- Detect a Contiguous Subarray with Target SumMeta · PHONE SCREENMeta interview questions
- Count Subarrays With Target SumAmazon · PHONE SCREEN
- Count Subarrays with Sum KByteDance · PHONE SCREENByteDance interview questions
- Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Longest Equal-Zero-One SubarrayMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
- Count Subarrays That Add Up to kAdobe · PHONE SCREENAdobe interview questions
- Find a Target-Sum SubarrayMorgan Stanley · PHONE SCREENMorgan Stanley interview questions