Lexicographically Smallest End Reversal
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 September 2024 looks like a string brute-force problem, and the trap is in what counts as a candidate. You reverse a prefix or a suffix, of any nonempty length, and return the smallest result. Length-one reversals return the original string, so the original is always a candidate. Miss that and you'll fail the "abcd" example. With n up to 2000, the plain approach fits. If you blank on the live OA, StealthCoder runs invisibly as a safety net, but this one is simple enough to write yourself once you see it.
The problem
For every nonempty prefix length, reverse that prefix and leave the rest unchanged. Also do the same for every nonempty suffix length. Return the lexicographically smallest string among all produced results. Function smallestEndReversal(text: String) → String Examples Example 1 text = "cba" return = "abc" Reversing the entire prefix yields abc. Example 2 text = "abcd" return = "abcd" A length-one reversal leaves the already smallest string unchanged. Constraints 1 <= text.length <= 2000 text contains lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there's no trick beyond enumeration. Generate every prefix reversal (n options) and every suffix reversal (n options), compare, and keep the minimum. Each candidate costs O(n) to build and O(n) to compare, so total work is O(n^2). At n = 2000 that's about 4 million character operations per pass, which is fine. The pitfall is the edge case. Reversing a prefix of length 1 or a suffix of length 1 returns the original text, and the full-string reversal appears in both families. Don't skip those or you'll drop valid candidates. Start your best answer as the original text, or include length 1 in your loops. Another pitfall is building strings with repeated concatenation in a slow language. Use slicing and reversed copies. If the clock is tight during the live OA, StealthCoder is the hedge that gets you the loop structure fast.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Lexicographically Smallest End Reversal 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Lexicographically Smallest End Reversal FAQ
What's the trick in the ZipRecruiter smallest end reversal problem?+
There isn't a clever one. Enumerate every prefix reversal and every suffix reversal, then take the minimum. The real catch is including the unchanged original, which comes from length-one reversals. Initialize your best answer to the input string and you're covered.
Will brute force pass with length up to 2000?+
Yes. You build 2n candidates, each O(n) to construct and compare. That's roughly 8 million character operations at most, which is comfortable. Don't over-engineer it with suffix arrays or anything fancy unless you have spare time.
Why does the example abcd return abcd?+
Every reversal either leaves the string unchanged (length one) or makes it larger, since it's already sorted. Reversing a prefix like ab gives bacd, which is bigger. So the original wins. This example exists to test that you keep the unchanged candidate.
Is the full reversal counted once or twice?+
It appears in both the prefix and suffix families, but it doesn't matter. You're taking a minimum, so duplicates change nothing. Just don't write logic that tries to deduplicate and accidentally removes a valid candidate.
How do I prepare for this in 48 hours?+
Practice string slicing and reversal in your language of choice, and write a loop that tracks a running minimum. Test with a sorted string, a reverse-sorted string, and a single character. That covers the edge cases this problem is built around.