Reported December 2025
Googlestring

Run-Length Encode a String

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Input up to 200000 characters means anything quadratic is dead on arrival, and this Google OA reported in December 2025 is built to punish it. The task is run-length encoding: collapse each maximal run of equal characters into the character plus its count. It's a string problem with one clean pass hiding inside it. The trap isn't the idea, it's the edge cases and the string building. If you blank on the loop boundaries during the live assessment, StealthCoder runs invisibly as a safety net and hands you the working solution.

The problem

Given a string s, replace every maximal run of equal consecutive characters with the character followed by the run's decimal length.
Concatenate the encoded runs in their original order and return the resulting string. Return an empty string when s is empty.

Function
runLengthEncode(s: String) → String

Examples
Example 1
s = "aaabbc"
return = "a3b2c1"
The maximal runs are aaa, bb, and c, with lengths 3, 2, and 1.
Example 2
s = "abcd"
return = "a1b1c1d1"
Every character forms a run of length one.
Example 3
s = ""
return = ""
There are no runs to encode.

Constraints
0 <= s.length <= 200000
s contains lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a single left-to-right scan. Keep the current character and a counter. When the next character differs, append the character and the count to an output buffer, then reset. After the loop, flush the final run, since forgetting that is the classic bug. Handle the empty string up front and return "". The pitfall at 200000 characters is building the result with repeated string concatenation in languages where that copies the whole string each time. Use a list or StringBuilder and join once. Counts can be multi-digit, so convert the number to a decimal string rather than casting it to a single character. That gives O(n) time and O(n) output space. Every run gets a count, even length one, so "abcd" becomes "a1b1c1d1". If the live OA rattles you and the off-by-one in the flush step slips, StealthCoder is the hedge that keeps the submission clean.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Run-Length Encode a 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google 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.

Run-Length Encode a String FAQ

How hard is the Google run-length encode question really?+

Easy on ideas, easy to lose points on details. The algorithm is one pass with a counter. Most failures come from skipping the last run, mishandling the empty string, or slow string concatenation on inputs near 200000 characters.

What's the trick to solving it fast?+

Scan once, track the current character and its count, and emit when the character changes. After the loop, emit the final run. Use a list or builder for output and join once at the end. That's the whole solution.

Do I write the count even when the run length is 1?+

Yes. The examples show "abcd" returning "a1b1c1d1". Every maximal run gets its character followed by its decimal length, no exceptions. Don't try to shorten it the way some real-world compressors do.

What edge cases should I test before submitting?+

Test the empty string, which returns an empty string. Test a single character, a string with all identical characters, and one with no repeats. Also test a run of 10 or more so you confirm multi-digit counts get written correctly.

How do I prepare for this in 48 hours?+

Write the one-pass loop from memory twice, once with an index and once with a grouping approach. Practice the final flush and the empty input. Then do a few similar string-scan problems so the counter reset pattern feels automatic.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it