Reported January 2024
Goldman Sachssliding window

Longest Subarray

Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Goldman Sachs OA. Under 2s to a working solution.
Founder's read

You're looking at a sliding window problem masquerading as dynamic programming. Goldman Sachs asked this in January 2024, and it's straightforward if you see the pattern: find the longest contiguous subarray whose sum is at most k. The trick is that you don't need DP at all. Two pointers and a running sum will solve it in one pass. If you blank on the approach during the OA, StealthCoder will feed you the window expansion logic in real time.

The problem

A subarray of an array 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 subarray 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. 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 Description Complete the function maxLength in the editor. 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):

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is a classic sliding window problem. Expand the right pointer to include elements, tracking the cumulative sum. When the sum exceeds k, shrink from the left until it's valid again. Record the maximum window size at each step. The greedy expansion-and-contraction approach is optimal because every element is visited at most twice. Common mistake: trying to use DP when a two-pointer scan is faster and cleaner. The pattern is sliding-window, not dynamic-programming, despite what the hint says. StealthCoder recognizes this instantly and can guide you through the pointer movements if you're stuck mid-OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Longest Subarray cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum size subarray sum equals k. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Goldman Sachs's OA.

Goldman Sachs reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Subarray FAQ

Is this actually a DP problem or a sliding window problem?+

Sliding window. The hint is a red herring. DP is overkill here. You maintain a window of valid subarrays, expand right, contract left when sum exceeds k, and track max length. One pass, no memoization needed.

What's the pitfall everyone hits?+

Trying to shrink the window with a for-loop instead of a while-loop. You must shrink incrementally until sum is valid again, not all at once. Also, updating max length after every contraction, not just when valid.

Can I solve this without sorting?+

Yes. Sorting isn't necessary. The subarray must be contiguous, so order matters. Sliding window works on the original array as-is. Sorting would break the contiguity requirement.

What if all elements are negative?+

Sliding window still works. Negative numbers only make it easier to stay under k. The rightmost element you can include before exceeding k is further right. The algorithm handles negatives naturally without special cases.

How do I code this in under 2 minutes?+

Initialize left = 0, sum = 0, max_len = 0. Loop right from 0 to n-1. Add a[right] to sum. While sum > k, subtract a[left] and increment left. Update max_len. Return max_len. Seven lines of logic.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Goldman Sachs.

OA at Goldman Sachs?
Invisible during screen share
Get it