Rightmost Longest Character Run
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this ZipRecruiter OA, reported in September 2022, is a single comparison operator. Write > when you need >= and you return the leftmost run instead of the rightmost. The problem itself is easy: scan a string, find the longest run of one repeated letter, return the letter plus its length like "c3". Tie-breaking is the whole trap. If you freeze on edge cases, StealthCoder runs invisibly during the live assessment as a safety net. Know the one-pass scan and you won't need it.
The problem
You are given a nonempty string text. A run is a maximal contiguous group of one repeated character. Find a run with maximum length. If several runs share that length, choose the rightmost one. Return the selected character followed immediately by the decimal run length. Function rightmostLongestRun(text: String) → String Examples Example 1 text = "bbacccd" return = "c3" The run ccc is longer than every other run. Example 2 text = "aabb" return = "b2" Both runs have length two, so the rightmost run bb wins. Constraints 1 <= text.length <= 100000 text contains lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is a single linear scan, not real dynamic programming even though it's hinted that way. Track the current run length. When the character matches the previous one, increment. Otherwise reset to 1. After each step, compare against the best length so far and update when current >= best. That >= is the rightmost rule: a later run with equal length overwrites the earlier one. Store the character and length at update time. The common pitfalls are using strict > and flipping the tie-break, forgetting to handle the final run if you only update on a change of character, and returning the length as a number instead of concatenating it as a string. With up to 100000 characters, O(n) time and O(1) space is what you want. Test with "aabb" and a one-character string. If you blank mid-assessment, StealthCoder is the hedge that gives you the loop while you keep your composure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Rightmost Longest Character Run 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rightmost Longest Character Run FAQ
How hard is the Rightmost Longest Character Run problem really?+
Easy. It's one pass over the string with a counter. The only real difficulty is the tie-break, where equal-length runs must resolve to the rightmost. Most failures come from a wrong comparison operator, not from the algorithm.
What's the trick to getting the rightmost run?+
Update your best answer when the current run length is greater than or equal to the best, not strictly greater. Because you scan left to right, a later run of equal length overwrites the earlier one, which is exactly the rightmost rule.
Do I need dynamic programming here?+
No. The hint says dynamic programming, but you only need the previous character and a running count. There's no table and no subproblem reuse. A simple loop with a few variables runs in O(n) time and O(1) extra space.
What edge cases should I test before submitting?+
Test a single character like "a", which should return "a1". Test "aabb", which should return "b2". Test a string that's all one letter, and a case where the longest run is the very last one, so you confirm the final run gets counted.
How do I prepare for this in 48 hours?+
Write the scan from scratch twice without looking. Then run the two given examples plus the edge cases by hand. Practice returning the character concatenated with the integer as a string. That's enough for a problem at this level.