FastPrepLongest Subarray
Problems/Longest Subarray

Longest Subarray

Goldman Sachs logoGoldman Sachs● EasyNEW GRADOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

A subarray of array a is defined as a contiguous block of a's elements having a length that is less than or equal to the length of the array. For example, the subarrays of array a = [1, 2, 3] are [1], [2], [3], [1, 2], [2, 3], and [1, 2, 3]. Given an integer, k = 3, the subarrays having elements that sum to a number ≤ k are [1], [2], and [1, 2]. The longest of these subarrays is [1, 2], which has a length of 2. Given an array, a, determine its longest subarray that sums to less than or equal to a given value k.

Function

maxLength(a: int[], k: int) → int

Complete the function maxLength in the editor below. The function must return an integer that represents the length of the longest subarray of a that sums to a number ≤ k.

maxLength has the following parameter(s):

  1. a[a[0],...a[n-1]]: an array of integers
  2. k: an integer

Examples

Example 1

a = [1, 2, 3]k = 4return = 2

The subarrays having elements that sum to a number ≤ 4 are [1], [2], and [1, 2]. The longest of these subarrays is [1, 2], which has a length of 2.

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ a[i] ≤ 10^3
  • 1 ≤ k ≤ 10^9

More Goldman Sachs interview questions

  • House Robber IIIPHONE SCREEN · Seen Nov 2026
  • Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
  • Maximum Ones After K OperationsOA · Seen Nov 2026
  • Palindrome CounterOA · Seen Oct 2026
  • Longest Palindromic SubsequenceOA · Seen Oct 2026
  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
  • Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Reduce Memory UsageAmazon · OA
  • Profit AnalysisVirtu Financial · OAVirtu Financial interview questions
  • Minimum Size Subarray SumMicrosoft · OAMicrosoft interview questions
  • Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Maximum Value for Two Delivery RobotsNuro · PHONE SCREENNuro interview questions
  • Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions
  • Longest Run of Ones After Flipping k ZerosC3.ai · PHONE SCREENC3.ai interview questions
  • Count Strictly Increasing Contiguous WindowsIBM · OAIBM interview questions
  • Minimum Positive Starting ValueBooking.com · OA
  • Array Interval SumsHeadlands Technologies · OAHeadlands Technologies interview questions
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
class Solution {
    public int maxLength(int[] a, int k) {
      // write your code here
    }

}
Checking account…