Minimum Operations to Make Alternating Binary String
Updated
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) → intExamples
Example 1
s = "11101"return = 1Flip 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 = 2Flip 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^5sconsists 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