FastPrepMaximum Ones After K Operations
Problems/Maximum Ones After K Operations

Maximum Ones After K Operations

Goldman Sachs logoGoldman Sachs● EasyNEW GRADINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report does not show the original function declaration. Our version is about 99% the same as the reported question.

Special thanks: 🌷 A special thank you to a lovely, amazing fren who shared that this question appeared again on November 11, 2026! ദ്ദി ˉ͈̀꒳ˉ͈́ )✧ 🩵

You are given a binary string s containing only '0' and '1'. You may perform at most k operations.

In one operation:

  1. Choose an index i such that 0 ≤ i < s.length - 1.
  2. 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 = 1return = 4

Choose i = 1. The string changes from "10110" to "11110", which contains 4 ones.

Example 2

s = "00011"k = 2return = 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'.

More Goldman Sachs Online Assessment questions

  • Palindrome CounterOA · Seen Oct 2026
  • Longest Palindromic SubsequenceOA · Seen Oct 2026
  • Count Redundant File BackupsOA · Seen Oct 2026
  • Minimum Distinct Prefix CostOA · Seen Oct 2026
  • Merge Overlapping IntervalsOA · Seen Sep 2026
  • Data ReorganizationOA · Seen Jul 2026
  • Alternating Parity PermutationsOA · Seen Jul 2026
  • Threshold AlertsOA · Seen Jul 2026
  • Find the Winner of an Array GameOA · Seen Jun 2026
  • Minimum Number of Taps to Water a GardenOA · Seen Jun 2026

Similar questions at other companies

  • Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
  • Prepare NotificationMicrosoft · OAMicrosoft interview questions
  • Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
  • Largest Number With Even Digit FrequenciesDRW · OADRW interview questions
  • Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
  • Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
  • Minimum Flips for Even Binary RunsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Missing WordsVanguard · OAVanguard interview questions
  • Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions
  • Smallest String After at Most One DeletionLSEG · OALSEG interview questions
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
class Solution {
    public int maximumOnes(String s, int k) {
      // Write your code here
    }
}
Checking account…