Run-Length Encoding
Reported by candidates from Zscaler's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zscaler reported this one in June 2024, and the detail that trips people is right in the statement: a singleton run still gets a count of 1. So "d" becomes "d1", and "zzzzzzzzzzzzx" becomes "z12x1" with a two-digit count. It's run-length encoding on a string up to 200000 characters, a single linear pass. If you've got an OA invite, this is a warm-up-tier problem that punishes sloppy edge cases. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment, but you probably won't need it once you've seen the shape of this.
The problem
Given a string text, encode each maximal run of equal consecutive characters from left to right. For every run, append the run's character followed by the full run length in base 10. Include the count 1 for a singleton run. Return the concatenated encoding. Return an empty string when text is empty. Function runLengthEncode(text: String) → String Examples Example 1 text = "aaabbccccd" return = "a3b2c4d1" The runs are aaa, bb, cccc, and d. Every run contributes its count. Example 2 text = "" return = "" An empty input has no runs. Example 3 text = "zzzzzzzzzzzzx" return = "z12x1" The first run shows a multi-digit count, and the singleton x still contributes 1. Constraints 0 <= text.length <= 200000 text contains printable ASCII characters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one pass with a run counter. Walk the string, compare each character to the previous one, and increment the count while they match. When the character changes, or you hit the end, append the character plus the count and reset. The pitfalls are all edge cases. Empty input must return an empty string, so guard before you touch index zero. Forgetting to flush the final run is the classic bug. Counts must be written in full base 10, so 12 is "12", not a single character like chr(12). Singletons need the 1. Use a list or StringBuilder and join at the end, because repeated string concatenation in a loop can go quadratic at 200000 characters. If you freeze on the flush logic during the live OA, StealthCoder can hand you the clean loop. Time is O(n), space is O(n) for the output.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Run-Length Encoding 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
You've seen the question.
Make sure you actually pass Zscaler's OA.
Zscaler 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 Encoding FAQ
How hard is the Zscaler Run-Length Encoding question really?+
Easy. It's a single pass over the string with a counter. The difficulty is entirely in edge cases: empty input, flushing the last run, singleton runs getting a 1, and multi-digit counts. If you handle those four, you're done in a few minutes.
What's the trick to getting it right the first time?+
Track the current character and a count. On a mismatch, append the character and count, then reset. After the loop ends, append the final run once more. Guard the empty string up front so you never index into nothing.
Do I need to worry about counts above 9?+
Yes. Example 3 has a run of 12 z's, which must output "z12". Convert the integer to a string and append it. Don't cast the number to a character, and don't cap the count at a single digit.
Will performance matter with 200000 characters?+
Slightly. The algorithm is O(n), which is fine. The danger is building the result with repeated string concatenation in languages where that copies each time. Collect pieces in a list or StringBuilder and join once at the end.
How should I prepare for this in 48 hours?+
Write it from scratch twice, then test the three examples plus a single-character string and a string with no repeats. Also try an all-same-character string to confirm the final run flushes. That covers every way this problem fails.