Reported September 2026
New Relicstring

Basic String Compression with Length Guard

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

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

The New Relic OA reported in September 2026 looks easy, and the input size is why people still lose points on it. Text can hit 100000 characters, so string concatenation in a loop will quietly punish you. It's a run-length encoding problem with one twist: only return the compressed form if it's strictly shorter. Equal length means you return the original. If you're staring at the invite and want a hedge, StealthCoder runs invisibly during the live assessment and gives you the solution if your mind goes blank.

The problem

Compress a string by replacing every maximal run of equal consecutive characters with the character followed by the run length in base 10.
Return the compressed representation only when it is strictly shorter than the original string. Otherwise, return the original string unchanged. The empty string remains empty.

Function
compressIfShorter(text: String) → String

Examples
Example 1
text = "aabcccccaaa"
return = "a2b1c5a3"
The run encoding is a2b1c5a3, which is shorter.
Example 2
text = "abcdef"
return = "abcdef"
The encoded form would be longer, so the original is returned.
Example 3
text = "aa"
return = "aa"
a2 has the same length as the input, so the input is returned.

Constraints
0 <= text.length <= 100000.
text contains printable ASCII characters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a single pass with two things tracked: the current character and its run count. When the character changes or the string ends, flush the char plus the count into a list or StringBuilder, then join once at the end. Don't use repeated += on a string, because at 100000 characters that can go quadratic in some languages. The common pitfalls are forgetting to flush the final run, handling the empty string, and using <= instead of < when comparing lengths. Example 3, "aa" becoming "a2", exists to catch that exact bug. Counts must be written in base 10, so a run of 12 is "12", two characters, not one. Compare the lengths at the end and return the original if the compressed form isn't strictly shorter. That's O(n) time. If you blank on the flush logic in the live OA, StealthCoder is the safety net that shows you a clean version.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Basic String Compression with Length Guard 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as string compression. If you have time before the OA, drill that.

⏵ The honest play

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

New Relic 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.

Basic String Compression with Length Guard FAQ

How hard is the New Relic string compression question really?+

Easy on paper. The logic is a single pass with a run counter. Most failures come from edge cases: the last run not being flushed, the empty string, and the strict length comparison. If you handle those three, you're done. Expect it to be a warm-up, not a filter.

What's the trick to get this right?+

Scan once, track the current character and its count, and append to a list or builder when the character changes. Flush the final run after the loop. Then compare lengths with strict less-than. Equal length means return the original string, as in the "aa" example.

Why does the input size matter if it's just one pass?+

With up to 100000 characters, repeated string concatenation can degrade to O(n^2) in some languages. Brute-force approaches that rebuild or slice strings each step get slow. Use a list or StringBuilder and join once. The algorithm stays O(n) in time.

What edge cases should I test before submitting?+

Test the empty string, which stays empty. Test a single character, where "a1" is longer so you return "a". Test "aa", where equal length returns the original. Test a run of 10 or more, since the count takes multiple digits. Test a string with no repeats, like "abcdef".

How do I prepare for this in 48 hours?+

Write run-length encoding from scratch two or three times until the flush logic is automatic. Then add the length guard and test your edge cases. It's the same pattern as the classic string compression problem. Focus on clean single-pass code rather than learning new techniques.

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

OA at New Relic?
Invisible during screen share
Get it