Consecutive Characters
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip away the wording and this Bloomberg question, reported in June 2021, is just "find the longest run of the same character." That's it. One pass, one counter. The title says Consecutive Characters, the function is maxConsecutivePower, and the only twist is the empty string returning 0. If your OA invite is sitting there and you're bracing for something nasty, relax. This one is a warm-up. Hinted pattern is sliding window, but a simple run counter does the same job. Get it clean, handle the edge case, and move on. If you freeze on the live OA, StealthCoder is the invisible backup that reads the problem and hands you the solution.
The problem
The power of a string is the maximum length of a nonempty contiguous substring containing only one repeated character. Return the power of text. Return 0 for an empty string. Function maxConsecutivePower(text: String) → int Examples Example 1 text = "abbcccddddeeeeedcba" return = 5 The longest run is five e characters. Constraints 0 <= text.length <= 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: walk the string once and compare each character to the previous one. If they match, increment a current run counter. If not, reset it to 1. After each step, update the best value with max(best, current). That's O(n) time and O(1) space, which easily handles length up to 10^5. Think of it as a window whose left edge jumps to the current index whenever the character changes. The common pitfalls are small. Forgetting the empty string and returning 1 instead of 0. Starting the counter at 0 and getting off-by-one results. Or updating the best only when the run breaks, so a run ending at the last character gets missed. Update on every step and that bug disappears. Nothing here needs a hash map or nested loops. If you blank during the live OA, StealthCoder runs invisibly on screen and gives you the one-pass solution to check against.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Consecutive Characters 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as consecutive characters. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Consecutive Characters FAQ
How hard is the Bloomberg Consecutive Characters question really?+
It's easy. One loop, one counter, one max. The only real risk is sloppy edge cases like the empty string. If you've written a run-length counter before, you can finish this in a few minutes and spend the rest of the time double-checking.
What's the trick to maxConsecutivePower?+
Compare each character to the one before it. Same means increment the run, different means reset to 1. Update the max on every iteration, not just when a run ends. That handles a run at the very end of the string.
Do I need a sliding window or is a simple counter enough?+
A simple counter is enough and it's the cleanest answer. A sliding window with two pointers works too, where the left pointer jumps when the character changes. Both are O(n) time and O(1) space. Pick whichever you can write without bugs.
What edge cases should I test before submitting?+
Test the empty string, which must return 0. Test a single character, which returns 1. Test a string where all characters are the same, and one where the longest run is at the end. Also test a string with no repeats, which returns 1.
How do I prepare for this in 48 hours?+
Write the one-pass counter from scratch twice, in your OA language. Then do a few easy string-scan problems so the loop pattern feels automatic. Don't over-study. This question rewards clean, careful code, not fancy algorithms.