Reverse Word Order in a Status Update Using Constant Extra Space
Last updated: November 29, 2026
Quick Overview
Given a status update as a mutable list of characters with possible leading, trailing, and repeated spaces, reverse the order of the words in place so the result has single spaces between words and no leading or trailing spaces, returning the new length.
Meta
November 29, 20260
0
0 solved
Given a status update as a mutable list of characters with possible leading, trailing, and repeated spaces, reverse the order of the words in place so the result has single spaces between words and no leading or trailing spaces, returning the new length.
A Meta phone screen often starts with a string manipulation warm-up and then asks for the in-place version. The interviewer checks the reverse-all-then-reverse-each-word trick and careful whitespace compaction.
What the Interviewer Expects
- Give the simple split and join answer first
- Then do it in place with O(1) extra space
- Compact spaces with a write pointer
- Reverse the whole buffer, then each word
- Return the new logical length
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you handle tabs and newlines as separators?
- How would you reverse only the first k words?
- Why does the double-reverse trick work?
- How would this behave with multi-byte Unicode characters?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of characters (letters, digits, punctuation, spaces). Words are maximal runs of non-space characters.
- Modify in place; the first
Lcharacters become the answer; returnL. - O(1) ex...
High-Level Design
Two steps. First compact: copy each word to a write pointer, inserting one space before every word except the first. That removes leading, trailing, and repeated spaces and gives length L.
Then rev...