FastPrepMinimum Nonadjacent Flips for an Alternating Binary String
Problems/Minimum Nonadjacent Flips for an Alternating Binary String

Minimum Nonadjacent Flips for an Alternating Binary String

Infosys logoInfosys● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Infosys new grad onsite interview. The report gives the bit-flip operation, a rule that flipped bits cannot be next to each other, and an alternating target. We added what to return when it is impossible, the limits, and the method interface. Our version is about 96% the same as the reported question.

You are given a non-empty binary string. In one operation, flip one bit. Across the entire transformation, no two flipped positions may be adjacent.

Return the minimum operations needed to make the string alternate between 0 and 1. Return -1 when neither alternating target can be reached under the nonadjacent-operation rule.

Function

minNonAdjacentFlips(bits: String) → int

Examples

Example 1

bits = "111101"return = 2

Flipping positions 0 and 2 produces 010101; the flipped positions are not adjacent.

Example 2

bits = "1001"return = -1

One target requires adjacent flips at positions 0 and 1, and the other requires adjacent flips at positions 2 and 3.

Constraints

  • 1 <= bits.length <= 200000
  • bits contains only 0 and 1.

Source note: Interview prompt excerpt preserved as source evidence.

More Infosys Onsite Interview questions

  • Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
  • Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
  • Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Insertion Steps to Make a String PalindromeONSITE INTERVIEW · Seen Nov 2026
  • Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
  • Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
  • Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
  • Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
  • Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026
  • Detect Substring In Any RotationONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
  • Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEWAnthropic interview questions
  • Shortest Sequence of Source SubsequencesPinterest · ONSITE INTERVIEWPinterest interview questions
  • Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
  • Minimum Removal for Balanced ParenthesesMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEWFlexport interview questions
  • Largest K-Digit SubsequenceGoogle · ONSITE INTERVIEWGoogle interview questions
  • Minimum Remove to Make Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Valid Parenthesis StringMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
All Infosys interview questionsSee Infosys hiring insights
Saved
class Solution {
    public int minNonAdjacentFlips(String bits) {
        // Write your code here.
    }

}
Checking account…