Reverse Odd-Position Words

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

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

Juniper Square reported this one in May 2025, and it looks friendly until the spaces bite. Reverse every word at an odd position, keep even words alone, and leave every space exactly where it was. If you're taking this OA in the next couple of days, the real test is leading, trailing and repeated spaces, not the reversal itself. The hinted pattern is two pointers, scanning the string once and flipping letter runs in place. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is very doable if you know where it breaks.

The problem

Given a string text, reverse the characters of each word at an odd one-based position and return the resulting string. Leave words at even positions unchanged.
For this exercise, assume a word is a maximal consecutive sequence of English letters, and only the literal space character separates words. Count words from left to right starting at position 1; spaces do not count as words.
For this exercise, assume every space must remain in its original position, including leading, trailing, and repeated spaces. Preserve letter case. An empty string or a string containing only spaces is returned unchanged.
For example, hello world again becomes olleh world niaga: the first and third words are reversed, while the second word stays unchanged.

Function
reverseOddPositionWords(text: String) → String

Examples
Example 1
text = "hello world again"
return = "olleh world niaga"
The first and third words occupy odd one-based positions.
Example 2
text = " AbC de FG "
return = " CbA de GF "
Spaces are preserved exactly, and spaces do not change the word counter. The first and third words are reversed with case preserved.
Example 3
text = ""
return = ""
There are no words to reverse.

Constraints
For this exercise, assume 0 <= text.length <= 20000.
For this exercise, assume text contains only uppercase and lowercase English letters and literal spaces.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to stop thinking in words split by spaces. Splitting and rejoining destroys repeated, leading and trailing spaces, which is the edge case that kills the naive solution. Instead, convert the string to a character array and walk it with an index i. Skip spaces. When you hit a letter, set j to the end of that run of letters, increment a word counter, and if the counter is odd, reverse the array between i and j-1 with two pointers swapping inward. Then jump i to j. Spaces never move, so they're preserved for free. Pitfalls: counting spaces as words, off-by-one on the run end, and mishandling the empty string or all-spaces input. Both fall out naturally from the loop. It's O(n) time with the character array as extra space. If the edge cases slip away under pressure, StealthCoder is the hedge during the live OA.

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 Reverse Odd-Position Words 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

⏵ The honest play

You've seen the question. Make sure you actually pass Juniper Square's OA.

Juniper Square 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.

Reverse Odd-Position Words FAQ

How hard is Reverse Odd-Position Words really?+

Easy to medium. The logic is a single pass with a word counter and a reversal. The difficulty is purely in edge cases: leading, trailing and repeated spaces, plus empty or all-space input. If you handle those, the rest is mechanical.

What's the trick to this problem?+

Don't split on spaces. Work on a character array, scan for runs of letters, count each run as a word, and reverse in place with two pointers when the count is odd. Spaces never move, so they stay exactly as given.

Why does the naive split-and-join approach fail?+

Splitting on a space and rejoining collapses or misplaces repeated, leading and trailing spaces. The problem requires every space to stay in its original position, so the output length and spacing would be wrong on inputs like the second example.

What should I test before submitting?+

Test the empty string, a string of only spaces, a single word, multiple spaces between words, and leading or trailing spaces. Also check mixed case like AbC reversing to CbA. These cover nearly every failure mode for this problem.

How do I prepare for this in 48 hours?+

Practice in-place two-pointer reversal on a character array and a scan loop that finds the start and end of each letter run. Write it once from scratch, then run the tricky space cases by hand. That's enough for this problem.

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

OA at Juniper Square?
Invisible during screen share
Get it