Balance the Game Board
Updated
Problem statement
A game board consists of N+1 fields, numbered from 0 to N from left to right. One letter ("a" or "b") is written between every two adjacent fields. The letters on the board are described by a string L of length N, where L[K] (for K within the range [0..N-1]) is the letter between fields K and K+1.
For example, given L = "aaabab" and N = 6, the game board at the beginning looks like this:
a a a b a b
0 1 2 3 4 5 6
A game piece is placed at start. It can move left or right, flipping the letter it crosses ("a" → "b" and "b" → "a"). The objective is to find the minimum number of moves needed to balance the count of "a" and "b". If it's impossible, return -1
Write a function that, given a string L of length N and an integer start, returns the minimum number of moves such that, after those moves, there will be the same number of letters "a" and "b" on the board (or returns -1 if it is impossible).
Function
balanceGameBoard(L: String, start: int) → intExamples
Example 1
L = "aaabab"start = 0return = 1The game piece must move one field to the right. This way, the first letter of L will be switched to produce string "baabab". Both letters occur three times in this string.
Example 2
L = "aaabab"start = 6return = 5The game piece has to move 5 times to the left: "aaabab" → "aaaaba" → "aaabba" → "aababa" → "aabbaa" → "abbbaa" Output: 5
Example 3
L = "ababa"start = 1return = -1It is impossible to equalize the number of letters "a" and "b". Output: -1
Example 4
L = "babbaa"start = 2return = 0The number of letters "a" and "b" is already equal.
Constraints
- N is an integer within the range [1..100].
- L is made only of the characters 'a' and/or 'b'.
- start is an integer within the range [0..N].
More Microsoft interview questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Authentication SystemOA · Seen Nov 2026
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Format a Centered Newspaper PageVisa · OAVisa interview questions
- Minimum Steps on a Circular Letter DialIBM · OAIBM interview questions
- Word Wrap with Even JustificationSentry · OA · PHONE SCREENSentry interview questions
- Max Number of OperationsSalesforce · OASalesforce interview questions
- Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEWFlexport interview questions
- Choose the Next Hidden-Word GuessCharacter.AI · PHONE SCREEN
- URL Segment Compression Part 3 — Global Token BudgetStripe · ONSITE INTERVIEWStripe interview questions
- Text JustificationUber · PHONE SCREENUber interview questions
- Tennis Game ScoreAbridge · OAAbridge interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)