Shortest Subarray with Sum at Least K
Updated
Problem statement
Given an integer array nums and an integer k, return the length of the shortest nonempty contiguous subarray whose sum is at least k. Return -1 if no such subarray exists.
Values may be negative. A subarray is a contiguous part of the array.
Function
shortestSubarray(nums: int[], k: int) → intExamples
Example 1
nums = [1]k = 1return = 1The single-element subarray has sum 1.
Example 2
nums = [1,2]k = 4return = -1Even the sum of the entire array is below 4.
Example 3
nums = [2,-1,2]k = 3return = 3Only the entire array reaches a sum of 3.
Constraints
1 <= nums.length <= 100000.-100000 <= nums[i] <= 100000.1 <= k <= 10^9.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Subarray with Sum at Least KGoldman Sachs · PHONE SCREEN
- Shortest Subarray with Sum at Least KOracle · ONSITE INTERVIEW
- Minimum Size Subarray Sum With NegativesTekion · ONSITE INTERVIEW
- Shortest Subarray with Sum at Least KApple · ONSITE INTERVIEW
- Profit AnalysisVirtu Financial · OA
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Sliding Window MaximumGroww · PHONE SCREEN
- Largest Value in Every Sliding WindowAdobe · PHONE SCREEN
- Sliding Window MaximumBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Fixed Worker Pool with a Shared QueueSquarepoint Capital · PHONE SCREEN