Rearrange a String Outside In
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The "abcdef" becoming "afbecd" example is the whole ZipRecruiter question, and it was reported in October 2022. You take the first character, then the last, and keep walking inward. If the length is odd, the middle character goes last. It's a string problem with a two-pointer shape, and it's easier than the wording makes it sound. If you have the OA in a day or two, the job is clean pointer handling and no off-by-one slips. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but you probably won't need it for this one.
The problem
You are given a string text. Build a new string by taking the first remaining character, then the last remaining character, and continuing inward. If the length is odd, append the middle character last. Function outsideIn(text: String) → String Examples Example 1 text = "abcdef" return = "afbecd" The pairs are a/f, b/e, and c/d. Example 2 text = "abcde" return = "aebdc" The pairs are a/e and b/d, followed by the middle c. Constraints 0 <= text.length <= 100000
Reported by candidates. Source: FastPrep
Pattern and pitfall
Put one pointer at the start and one at the end. While left is less than right, append text[left], then text[right], and move both inward. When the loop ends, if left equals right, you've landed on the middle character of an odd-length string, so append it. That's the entire algorithm, O(n) time. The common pitfall is performance. With length up to 100000, repeated string concatenation in some languages goes quadratic, so collect characters in a list or builder and join once. The second pitfall is the empty string. Length 0 should return an empty string, and the loop handles it naturally if you don't index first. Check the odd case against "abcde" giving "aebdc". If you freeze on the live OA, StealthCoder can hand you this pointer loop in seconds, but write it yourself first.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Rearrange a String Outside In 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.
Rearrange a String Outside In FAQ
How hard is the ZipRecruiter Rearrange a String Outside In question really?+
Easy. It's a two-pointer walk with one edge case, the middle character on odd lengths. If you can trace "abcde" to "aebdc" by hand, you can code it. The only real risk is sloppy indexing or slow string building.
What's the trick to solving it?+
Use left and right pointers moving toward each other. Append the left character, then the right one, each iteration. After the loop, if the pointers meet on the same index, append that middle character. No sorting or extra data structures needed.
Will a naive solution time out at 100000 characters?+
It can. Building the result by repeated concatenation may copy the string every time in some languages, which gets quadratic. Push characters into an array or string builder and join once at the end. That keeps it linear.
What edge cases should I test?+
Test the empty string, a single character, two characters, and one odd and one even example. Empty should return empty. A single character should return itself. Two characters like "ab" should return "ab". Those cover every pointer outcome.
How do I prepare for this in 48 hours?+
Don't binge problems. Write this one from scratch twice in your language, once with an index loop and once with two pointers. Then do a couple of similar string reorder problems. The pattern is simple, so speed and clean edge handling matter most.