Minimum Additions to Make a Valid ABC String
Reported by candidates from IBM's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
IBM reported this one in September 2026, and the input size is the first thing to read. With word.length up to 10^5, you can't try inserting characters and checking validity. You need one clean pass. The problem is a string scan: turn a word of a, b, and c into repeated "abc" blocks with the fewest insertions. If you have the OA coming up, this is a pattern you can finish in ten lines once you see it. StealthCoder sits invisible on your screen as a safety net if you blank during the live assessment, but the idea here is small enough to hold in your head.
The problem
Given a string word containing only 'a', 'b', and 'c', return the minimum number of characters that must be inserted so that the resulting string is valid. A string is valid when it is formed by concatenating one or more copies of "abc". You may insert a character at any position, but the existing characters must remain in their original order. Function addMinimum(word: String) → int Examples Example 1 word = "b" return = 2 Insert 'a' before 'b' and 'c' after it to obtain "abc". Example 2 word = "aaa" return = 6 Each 'a' starts a separate cycle, so insert 'b' and 'c' after each one to obtain "abcabcabc". Example 3 word = "abcabc" return = 0 The input already consists of two complete "abc" cycles. Constraints 1 <= word.length <= 10^5. Every character in word is 'a', 'b', or 'c'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: count how many "abc" cycles the string needs, then subtract what's already there. Walk the string and track the previous character. A new cycle starts whenever the current character is less than or equal to the previous one, because 'a' < 'b' < 'c' must strictly increase inside a single cycle. Count cycles, including the first. The answer is cycles * 3 - word.length. Check it on "aaa": 3 cycles, 9 minus 3 gives 6. On "abcabc": 2 cycles, 6 minus 6 gives 0. The common pitfall is simulating insertions or building the final string, which wastes memory and invites off-by-one errors. Another is forgetting to count the first character as a cycle start. It's O(n) time and O(1) space. If you freeze live, StealthCoder can hand you this counting formula from the screen, but it's worth memorizing.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Additions to Make a Valid ABC String cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass IBM's OA.
IBM reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Additions to Make a Valid ABC String FAQ
How hard is the IBM Minimum Additions to Make a Valid ABC String question really?+
Easy to medium. Once you see that each cycle needs exactly three characters, it's a single loop and a subtraction. Candidates lose time by trying to simulate insertions or build the output string. The code is short, the insight is the whole problem.
What's the trick to solving it in one pass?+
Count cycle starts. A new cycle begins whenever the current character is not greater than the previous one, plus the very first character. Then return cycles * 3 minus the word length. No string building, no extra data structures needed.
Will brute force pass with length up to 10^5?+
No. Trying insertion positions and validating each result blows up combinatorially, and even a single simulation that rebuilds strings repeatedly risks timing out. The linear scan with a counter is the intended approach and uses constant extra space.
What edge cases should I test before submitting?+
Test a single character like "b" (answer 2), all repeats like "aaa" (answer 6), an already valid string like "abcabc" (answer 0), and descending input like "cba". The descending case checks that you start a new cycle on every non-increasing step.
How do I prepare for this in 48 hours?+
Practice the cycle-counting idea on two or three similar string-scan problems, then write this one from memory twice. Focus on the comparison rule (current <= previous starts a new cycle) and the formula. That covers it, and the pattern shows up in other string-completion questions.