Maximize Element Frequency
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at this Goldman Sachs problem is reaching for a frequency map. It's tagged hash-table, but the real answer is sort plus sliding window. Goldman Sachs candidates reported it in September 2026, and the naive approach of trying every target value with a full scan dies on 10^5 elements. You get at most k increments, and you want the largest group of elements you can push up to one common value. If you freeze or blank on the window logic, StealthCoder is the invisible safety net running during the live OA. Know the trick first and you probably won't need it.
The problem
Given an array of positive integers nums and a positive integer k, you may perform at most k operations. In one operation, choose one index and increase nums[i] by 1. Return the maximum possible frequency of any value in the array after performing the operations. The frequency of a value is the number of times it appears. Function maxFrequency(nums: int[], k: int) → int Examples Example 1 nums = [1,3,5,7,8,9,10,15] k = 6 return = 4 Increase 7 three times, 8 twice, and 9 once. The array then contains four occurrences of 10, using exactly 6 operations. Example 2 nums = [1,2,4] k = 5 return = 3 Increase 1 to 4 using 3 operations and increase 2 to 4 using 2 operations. All three elements become 4. Example 3 nums = [1,4,8,13] k = 5 return = 2 Two elements can be made equal, for example by increasing 8 to 13. Making any three elements equal requires more than 5 operations. Example 4 nums = [3,9,6] k = 2 return = 1 No value can reach another array value with only 2 increments, so the maximum frequency remains 1. Constraints 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^5 1 <= k <= 10^5
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sort the array. Increments only go up, so the best target for any group is the largest element in that group, and the group should be a contiguous run in sorted order. Slide a window with right pointer r. The cost to lift everything in the window to nums[r] is nums[r] * windowSize - windowSum. While that cost exceeds k, move the left pointer and shrink the sum. Track the max window size. That's O(n log n) for the sort and O(n) for the scan. The common pitfall is the hash map approach, which counts existing frequencies and ignores that the target may already be in the array but groups still need contiguous neighbors. Another trap is integer overflow in nums[r] * size, so use 64-bit in languages that need it. If the window condition slips from your head mid-OA, StealthCoder can hand you the template while the proctor sees nothing.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Maximize Element Frequency 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as frequency of the most frequent element. If you have time before the OA, drill that.
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.
Maximize Element Frequency FAQ
What's the trick in Maximize Element Frequency?+
Sort first, then use a sliding window. The target for any group is its largest element, so the cost to equalize the window is nums[r] * size - sum. Shrink from the left while cost exceeds k. The answer is the largest window size you ever hold.
Why not just use a hash map since it's tagged hash-table?+
A frequency map tells you what exists, not what's cheap to reach. You'd need to test every target against every other element, which is O(n^2) at 10^5 elements. Sorting turns the neighbors into a contiguous range, and that's what makes the linear window possible.
How hard is this really for the Goldman Sachs OA?+
Medium. The idea is short once you see it, but candidates burn time on the wrong approach. If you recognize sort plus window within a few minutes, the code is about fifteen lines. Edge cases are small: single element, and k too small to merge anything.
What edge cases should I test before submitting?+
Test Example 4, where nothing can merge and the answer is 1. Test a single-element array. Test all equal values, where cost is zero and the window covers everything. Also check large values with big window sizes so your cost math doesn't overflow in a 32-bit type.
How do I prepare for this in 48 hours?+
Write this one solution from scratch twice, then do one or two other sort-plus-window problems. Practice the invariant: window cost equals target times size minus sum. Time yourself to code it cleanly in under 15 minutes, and rehearse explaining why the target is always the right endpoint.