FastPrepMinimum Operations to Make Alternating Binary String
Problems/Minimum Operations to Make Alternating Binary String

Minimum Operations to Make Alternating Binary String

IBM logoIBM● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a binary string s. In one operation, you pick any bit and flip it: a 0 becomes 1, or a 1 becomes 0.

The string is alternating when no two neighboring characters are the same. Return the minimum number of operations needed to make s alternating.

Function

solveBinaryString(s: String) → int

Examples

Example 1

s = "11101"return = 1

Flip the bit at index 1 (indices start at 0). The string becomes "10101", which alternates, so the answer is 1.

Example 2

s = "111101"return = 2

Flip the 1st and 3rd characters (1-based indexing). The string becomes zero, one, zero, one, zero, one, which alternates. The other alternating string, "101010", would need four flips. So the answer is 2.

Constraints

  • 0 ≤ length of s ≤ 2 * 10^5
  • s consists only of characters '0' and '1'

More IBM interview questions

  • Missing IntervalsOA · Seen Nov 2026
  • Count Peaks and ValleysOA · Seen Nov 2026
  • Last Object Exit TimeOA · Seen Nov 2026
  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
  • Alternate Odd and Even Numbers with Two GoroutinesPHONE SCREEN · Seen Nov 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Nov 2026
  • Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
  • Longest Palindromic SubstringPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
  • 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
  • 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

IBM interview experiences

  • IBM Full-Time SWE OA Questions: 1 Experience
  • IBM Software Intern Résumé Rounds: 2 Experiences (Part 3 of 3)
  • IBM Intern Blitz Interviews: 23 Experiences (Part 2 of 3)
  • IBM Software Intern Interviews: 44 Experiences (Part 1 of 3)
  • IBM Software Intern Interview Questions: 15 Experiences
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public int solveBinaryString(String s) {
      // write your code here
    }

}
Checking account…