Longest Contiguous Character Run
Reported by candidates from Boston Consulting Group's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The rightmost tiebreak is the detail that trips people on this Boston Consulting Group OA, reported in July 2026. You get a lowercase string, find the longest run of one repeated character, and return it as the character plus its count, like "c3". It looks trivial, and it mostly is. The danger is a single off-by-one on ties or on the final run. If you've got the OA coming in a day or two, this one is about clean single-pass code, not clever theory. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
You are given a non-empty string source consisting of lowercase English letters. Find the longest contiguous substring that consists of the same character. If several such substrings have the same maximum length, choose the rightmost one. Return a string containing the chosen character followed by the number of times it occurs in that substring. A solution with time complexity no worse than O(source.length^2) fits within the execution time limit. Function solution(source: String) → String Examples Example 1 source = "bbacccdbbab" return = "c3" The character a appears in two runs of length 1. The character b appears in three runs: two of length 2 and one of length 1. The single c run has length 3, so it is the longest run and the result is "c3". Example 2 source = "bbaacaa" return = "a2" The runs "bb", the first "aa", and the final "aa" all have the maximum length 2. The final "aa" is the rightmost maximum run, so the result is "a2". Constraints For this exercise, source.length >= 1. source consists only of lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The hinted pattern says dynamic-programming, but it's really one linear scan with a running counter. Walk the string, and if the current char equals the previous one, increment the current run length. Otherwise reset it to 1. After each step, compare the current run to the best so far. The trick is the comparison operator. Use >= instead of > so a later run of equal length overwrites the earlier one, which gives you the rightmost maximum. Example 2, "bbaacaa", is exactly this test: the answer must be "a2" from the last run, not "b2". The common pitfall is forgetting to check the final run when the string ends, or building the output with the wrong order. Always return the char first, then the count as a string. The statement allows O(n^2), but O(n) is just as easy. If you freeze live, StealthCoder can hand you this loop in seconds.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Longest Contiguous Character Run 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 passed his OA cold and still thinks the filter is broken.
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 Boston Consulting Group's OA.
Boston Consulting Group reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Contiguous Character Run FAQ
How hard is the Longest Contiguous Character Run question really?+
Easy. It's a single pass with a counter. The statement even allows O(n^2), so brute force passes too. The only real risk is the rightmost tiebreak and handling the last run, both of which come down to small details rather than hard algorithms.
What's the trick to getting the rightmost run?+
Update your best answer when the current run length is greater than or equal to the best length, not just greater. That way every later run of equal length replaces the earlier one. Example 2, "bbaacaa" returning "a2", checks exactly this.
Do I need dynamic programming for this?+
No. The hint says dynamic-programming, but a running run-length variable is all you need. You only depend on the previous character, so there's no table. Keep current length, best length, and best character, then update as you scan.
What edge cases should I test before submitting?+
Test a single-character string, a string where all characters are the same, and a string where every run has length 1, where the rightmost char wins. Also test ties like "bbaacaa" and make sure the final run is compared after the loop or inside it.
How do I prepare for this in 48 hours?+
Write the linear scan from scratch twice and check it against both examples. Then practice similar run-length problems like string compression. Focus on loop boundaries and tiebreak operators, since those are where easy problems like this one actually fail.