Recursively Sum Identical Digit Runs
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A ZipRecruiter OA reported in September 2022 hands you a digit string up to 100000 characters and says collapse it until nothing repeats. That input size is the whole point. A lazy rebuild of the string on every pass, with a nested scan for each run, is how you time out on the big cases. The task is run-length compression with a twist: each run turns into digit times length, and the new digits can glue into fresh runs. It's simulation with a bit of math on top. If you blank mid-assessment, StealthCoder sits invisibly on your screen and gives you a working solution as a safety net. Know the shape before you open the invite.
The problem
Given a decimal digit string, replace every maximal repeated-digit run by the decimal representation of digit × run length. Single-character runs remain that digit. Repeat whole passes when a replacement creates adjacent equal digits, stopping when all adjacent digits differ. Return the final string. Function compressDigitRuns(digits: String) → String Examples Example 1 digits = "66644319333" return = "26328" Repeated passes collapse the newly adjacent digits until no equal neighbors remain. Example 2 digits = "123" return = "123" No run needs replacement. Constraints 1 <= digits.length <= 100000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to treat one pass as a single linear scan. Walk the string, count each maximal run, compute digit times count, and append that number's decimal digits to a builder. Never use string concatenation in a loop. Build a list and join it once. Then compare the result to the input. If they match, no adjacent equal digits remain, so you stop. If they differ, run another pass. The common pitfall is merging runs across the boundaries of replaced output within the same pass. Don't do that, because the statement says whole passes. Another pitfall is forgetting that a single character stays as that digit, not digit times one with a different format. Test a run of nine or more, since the product can have two or three digits. If the logic slips under pressure, StealthCoder is the hedge on the live OA. Check the stop condition and the rebuild cost before submitting.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Recursively Sum Identical Digit Runs 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Recursively Sum Identical Digit Runs FAQ
What's the core trick in the ZipRecruiter digit run problem?+
Do each pass as one linear scan. Count maximal runs, replace each with the decimal string of digit times length, and keep single characters as they are. Then repeat until the output equals the input. Use a list of pieces and join once, not repeated string concatenation.
How do I know when to stop repeating passes?+
Stop when a full pass produces a string identical to its input, or equivalently when no two adjacent digits are equal. Checking equality of the before and after strings is the simplest and least error-prone check, and it costs one linear comparison.
Will a naive approach pass with length 100000?+
A single pass is linear, so that part is fine. The danger is slow string building or doing extra scans per run. Keep each pass O(n) with a builder. Since output length never exceeds input length for runs of two or more, the string doesn't blow up between passes.
What edge cases should I test before submitting?+
Test a one-character string, a string with no repeats like 123, a long single-digit run, a run whose product has two digits, and a case where the replacement creates a new adjacent equal pair. Those cover the stop condition, formatting, and the repeated-pass logic.
How should I prepare for this in 48 hours?+
Code the one-pass run compression from scratch twice, then wrap it in a loop with the equality stop check. Hand-trace the first example through each pass. That's enough. This is a simulation problem, so careful tracing beats reading more theory.