Expand Decimal Digits into Runs of Ones
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ZipRecruiter OA reported in November 2021 hands you a rule that fits in one breath: every digit d becomes d copies of the character 1, and everything else stays put. "x0y2" turns into "xy11" because zero vanishes. It's a single-pass string build, nothing fancy. The danger isn't difficulty, it's sloppy handling of zero, multi-digit runs like "12", and slow string concatenation on a 100000-character input. If you blank on the details, StealthCoder can run invisibly as a safety net during the live assessment. Most candidates won't need it here.
The problem
Scan text from left to right. Replace each decimal digit d with exactly d copies of the character 1. Keep every nondigit character unchanged. Each digit is interpreted independently, so 0 contributes no characters. Function expandDigitsToOnes(text: String) → String Examples Example 1 text = "a5b3de" return = "a11111b111de" Each digit expands independently while letters remain in place. Example 2 text = "x0y2" return = "xy11" Zero contributes an empty run and two contributes two ones. Constraints 0 <= text.length <= 100000 text contains ASCII letters and decimal digits.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a straight left-to-right scan with an output buffer. For each character, check if it's a digit. If it is, convert it to an integer and append that many 1s. If not, append the character unchanged. The trap is treating "12" as twelve. The statement says each digit is interpreted independently, so "12" becomes "1" plus "11", which is three ones. The second trap is performance. Repeated string concatenation in some languages is quadratic, so use a list, StringBuilder, or array and join once at the end. Output can reach 9 times the input length, about 900000 characters, which is fine. Empty input returns an empty string. Zero adds nothing. Total work is linear in output size. If the live OA rattles you and you stall on the buffer setup, StealthCoder is the hedge that gets you a clean linear solution fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Expand Decimal Digits into Runs of Ones 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Expand Decimal Digits into Runs of Ones FAQ
How hard is the ZipRecruiter digit expansion problem really?+
Easy. It's one pass over the string with a conditional. The only real risks are misreading multi-digit numbers as a single value and using slow string concatenation. If you code it in under ten minutes with a builder, you're fine.
What's the trick to expandDigitsToOnes?+
There isn't a deep one. Treat each digit independently, append d ones for a digit, append the character otherwise. Use a list or StringBuilder and join at the end. Remember that "0" appends nothing and "12" yields three ones.
Does "12" become twelve ones?+
No. The statement says each digit is interpreted independently. So "1" gives one 1 and "2" gives two 1s, for three ones total. Don't parse consecutive digits into a number. That's the most likely wrong answer.
What edge cases should I test before submitting?+
Test the empty string, a string of only zeros, a string with only letters, consecutive digits like "909", and a max-size input of 100000 nines. The last one checks that your output building doesn't time out.
How do I prepare for this in 48 hours?+
Practice a few basic string-building problems in your language of choice. Know its efficient string builder, how to convert a digit character to an int, and how to repeat a character. That covers everything this problem needs.