Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Split on whitespace (dropping empties), reverse the array of words, and join with a single space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you operate at word granularity and normalise messy spacing.
Where candidates slip
Character reversal, or empty tokens from a bad split.
How to stand out
Mention the in-place O(1)-space version: reverse whole, then reverse each word.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Split, reverse, join
1Hint 1Explore hintHide hint
Reverse words, not characters — what's the unit you operate on?
2Hint 2Explore hintHide hint
Split on whitespace, reverse the list, join with single spaces.
3Hint 3Explore hintHide hint
Trim and split on \s+ so extra spaces don't make empty tokens.
A · Core idea
Reverse the order of words (not characters): tokenise on whitespace, reverse the list of words, and join with single spaces.
Why tokenise → reverse → join. Splitting on runs of whitespace turns the sentence into clean word tokens (dropping extra spaces); reversing the token list flips word order; joining with one space normalises the output spacing. Each step is simple and composes cleanly.
Why `trim()` + split on `\s+`. Leading/trailing spaces would otherwise create empty tokens; trimming and splitting on whitespace runs avoids them so the output has no stray spaces.
Don't reverse characters. Reversing the characters of the whole string would also reverse each word's spelling — a different (wrong) result. The unit of reversal here is the word.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "the quick fox"split
[the, quick, fox]
tokens"fox quick the"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(n) space — tokenising, reversing, and joining are each linear in the input length.
E · Edge cases and pitfalls5s = "hello" — One word → "hello".Explore edge casesHide edge cases
Edge cases handled
s = "hello"One word → "hello".s = " a b "Extra spaces normalised → "b a".s = "a b c"→ "c b a".
Pitfalls
- Reversing characters instead of words.
- Leaving double spaces from a naive split.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String reverseWords(String s) { String[] words = s.trim().split("\\s+"); StringBuilder sb = new StringBuilder(); for (int i = words.length - 1; i >= 0; i--) { sb.append(words[i]);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Reversing characters instead of words.
- Leaving double spaces from a naive split.
Revise
Carry the pattern into the interview
Pattern to remember
Tokenise → reverse → join
Code pattern to memorize
' '.join(s.split()[::-1])Rules to remember
- 1Split on \s+.
- 2Reverse the word list.
- 3Join with single spaces.
Use this pattern when
- Word-order manipulation
Interview takeaway
Word-level operations start by tokenising into words.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Tokenise → reverse → join. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Reverse Words in a StringComing soonIn-place: reverse all, then each word.
- Reverse a StringCharacter-level reversal.