Equal-Length Character Blocks
Reported by candidates from Deloitte's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Deloitte OA, reported January 2022, is trying to be clever about the target length. Candidates start debating averages or the smallest block, then burn their time. It's a plain string scan. Split s into maximal runs of 'a' or 'b', find the longest run, and pad every other run up to that length. If you freeze on the OA, StealthCoder runs invisibly as a safety net and hands you the clean solution while the proctor sees nothing.
The problem
Given a string s containing only 'a' and 'b', split it into maximal consecutive blocks of equal characters. You may add letters only at the beginning or end of an existing block. Return the minimum number of letters that must be added so that every block has the same length. Function minAdditionalLetters(s: String) → int Examples Example 1 s = "babaa" return = 3 The blocks have lengths 1, 1, 1, 2. Add one letter to each of the first three blocks to obtain blocks of length 2. Example 2 s = "bbbab" return = 4 The blocks have lengths 3, 1, 1. Add two letters to each shorter block. Example 3 s = "bbbaaabbb" return = 0 All three blocks already have length 3. Constraints 1 <= s.length <= 40,000 s contains only 'a' and 'b'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: you can only add letters at block ends, and adding the same letter just extends the block. So the target length can't be smaller than the longest existing block, and the longest block is also the cheapest target. Any bigger target costs more for every block. Answer = (max block length * number of blocks) - s.length. One pass, O(n) time, O(1) space. The common pitfall is merging blocks by accident, or thinking you must insert the opposite letter, which would split a block and change the count. Another is off-by-one when closing the final run after the loop ends. Check example 2: blocks 3,1,1, max 3, three blocks gives 9, minus length 5 is 4. Correct. If you blank on the loop shape during the live OA, StealthCoder is the hedge that gives you the run-counting code fast.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Equal-Length Character Blocks 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Deloitte's OA.
Deloitte reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Equal-Length Character Blocks FAQ
What's the trick in Equal-Length Character Blocks?+
Use the longest existing block as the target. Padding to anything larger only costs more. The answer is max run length times number of runs, minus the string length. You never need to simulate the additions, just count runs and track the max.
How hard is this Deloitte question really?+
Easy. It's a single pass over a string with a counter for the current run and a max. The only real difficulty is overthinking it. Most of the work is handling the last run correctly after the loop ends.
What edge cases should I test?+
A single character string returns 0. A string where all characters are the same is one block, so 0. Alternating characters like abab gives every block length 1, so 0. Also test a final block that is the longest, since that's where off-by-one bugs hide.
What complexity is expected with 40,000 characters?+
O(n) time and O(1) extra space is the clean answer. With length up to 40,000, even a sloppy O(n log n) passes, but there's no reason to build lists of blocks. Just track the count of blocks and the max length while scanning.
How do I prepare in 48 hours for a string question like this?+
Practice run-length scanning until it's automatic: compare s[i] to s[i-1], extend or reset, update the max. Then write the formula from memory and check it against the three examples. Twenty minutes of that covers this pattern.