FastPrepShortest Subarray with Sum at Least K
Problems/Shortest Subarray with Sum at Least K

Shortest Subarray with Sum at Least K

Goldman Sachs logoGoldman Sachs● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A Goldman Sachs CoderPad report links the exact Shortest Subarray with Sum at Least K exercise and says the test cases matched. This practice version is an approximately 99% match.

Given a signed 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.

Function

shortestSubarrayAtLeastK(nums: int[], k: long) → int

Examples

Example 1

nums = [2,-1,2]k = 3return = 3

Only the entire array reaches sum 3.

Example 2

nums = [1,2]k = 4return = -1

No subarray reaches 4.

Constraints

  • 1 <= nums.length <= 200000
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^18

More Goldman Sachs problems

  • House Robber IIIPHONE SCREEN · Seen Nov 2026
  • Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
class Solution {
    public int shortestSubarrayAtLeastK(int[] nums, long k) {
      // write your code here
    }
}
Checking account…