Maximum Ones After K Operations
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Trailing zeros are what sink most first attempts at this Goldman Sachs question, reported in August 2026. It reads like a propagation puzzle on a binary string, with operations that copy a neighbor's value leftward. It's really a counting problem with one nasty edge. Every operation can flip at most one zero, and some zeros can never flip, no matter how many operations you have. Miss that and your answer is wrong on the hidden tests while the samples pass. If you blank during the live OA, StealthCoder runs invisibly on your desktop and hands you the logic, so you're not guessing under pressure. Here's the trick before you open the invite.
The problem
You are given a binary string s containing only '0' and '1'. You may perform at most k operations. In one operation: Choose an index i such that 0 ≤ i < s.length - 1. Set s[i] = max(s[i], s[i + 1]). Each operation modifies only one position. Return the maximum possible number of '1' characters in the final string. Function maximumOnes(s: String, k: int) → int Examples Example 1 s = "10110" k = 1 return = 4 Choose i = 1. The string changes from "10110" to "11110", which contains 4 ones. Example 2 s = "00011" k = 2 return = 4 Use the two operations to propagate a '1' leftward: "00011" becomes "00111" and then "01111". The final string contains 4 ones. Constraints 1 ≤ s.length ≤ 2 * 10^5 0 ≤ k ≤ s.length s contains only '0' and '1'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Each operation sets s[i] to max(s[i], s[i+1]), so a 1 only moves left, and each operation creates at most one new 1. A zero is convertible only if some 1 exists somewhere to its right. Work right to left and every zero left of the last 1 has a 1 next to it by the time you reach it, so each costs exactly one operation. The answer is: original ones + min(k, zeros positioned before the last 1). Zeros after the last 1 are dead. If the string has no 1 at all, return 0 regardless of k. The pitfall is counting all zeros, or simulating operations one by one and chasing order. Simulation is unnecessary at n up to 2*10^5. One pass, O(n) time, O(1) space. If the formula slips your mind mid-assessment, StealthCoder is the hedge that surfaces it in seconds.
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 Maximum Ones After K Operations 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
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.
Maximum Ones After K Operations FAQ
What's the actual trick in Maximum Ones After K Operations?+
Each operation adds at most one 1, and only zeros that have a 1 somewhere to their right can be converted. So count zeros before the last 1, cap that by k, and add it to the existing ones. No simulation needed. It's a greedy count in a single pass.
Which edge cases break a naive solution here?+
An all-zero string must return 0, since there's no 1 to propagate. Trailing zeros after the last 1 must not be counted as convertible. Also k equal to 0 should return the original count of ones. Test those three before submitting.
How hard is this Goldman Sachs OA question really?+
Easy to medium. The code is about five lines once you see the rule. The difficulty is the reasoning: spotting that order doesn't matter if you fill right to left, and that trailing zeros are unreachable. Candidates who simulate tend to overcomplicate it.
What's the time complexity I should target?+
O(n) time and O(1) extra space. With a string length up to 2*10^5, anything quadratic, like repeating operations and rescanning, risks timing out. Scan once to count ones, find the last 1's index, and count zeros before it.
How do I prepare for this in 48 hours?+
Work through the two examples by hand and write the formula in your own words. Then write the solution from scratch and test the all-zeros, trailing-zeros, and k=0 cases. Also review similar greedy counting problems on binary strings so the pattern feels familiar.