151. Reverse Words in a String
mediumAsked at MicrosoftReverse Words in a String is Microsoft's go-to string-manipulation question. The clean two-liner (split, reverse, filter, join) is acceptable but interviewers grade highly on the in-place O(1) version that reverses the whole string then reverses each word.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Microsoft loops.
- Glassdoor (2026-Q1)— Microsoft Windows/Office org onsite reports list Reverse Words as a recurring 20-minute string medium.
- Blind (2025-11)— Microsoft new-grad and L60 reports flag Reverse Words with the in-place follow-up.
Problem
Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Constraints
1 <= s.length <= 10^4s contains English letters (upper-case and lower-case), digits, and spaces ' '.There is at least one word in s.
Examples
Example 1
s = "the sky is blue""blue is sky the"Example 2
s = " hello world ""world hello"Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3
s = "a good example""example good a"Explanation: Reduce multiple spaces between two words to a single space.
Approaches
1. Split + filter + reverse (idiomatic)
Split on whitespace, drop empty tokens (from collapsed multi-space), reverse the array, join with a single space.
- Time
- O(n)
- Space
- O(n)
function reverseWords(s) {
return s.trim().split(/\s+/).reverse().join(' ');
}Tradeoff: Cleanest one-liner. Acceptable at most companies, but Microsoft will ask for the in-place version on a follow-up because their interview rubric specifically tests whether you can manipulate a string buffer without allocating a new one.
2. Two-pointer parse without regex
Walk the string left to right, collect non-space chars into a word, push to a result array when a space is hit, reverse at the end.
- Time
- O(n)
- Space
- O(n)
function reverseWords(s) {
const words = [];
let i = 0;
while (i < s.length) {
while (i < s.length && s[i] === ' ') i++;
if (i === s.length) break;
let j = i;
while (j < s.length && s[j] !== ' ') j++;
words.push(s.slice(i, j));
i = j;
}
return words.reverse().join(' ');
}Tradeoff: Same complexity as the split version but no regex — useful when the interviewer says 'pretend you can't use split.' Makes the leading/trailing/multi-space handling explicit, which Microsoft graders like.
3. Reverse-then-reverse-each-word (in-place mental model)
Reverse the entire string, then reverse each word in place. Final pass to collapse extra spaces. With a char array this is O(1) extra space.
- Time
- O(n)
- Space
- O(1) for char array (true in C/C++ where strings are mutable)
function reverseWords(s) {
const chars = s.trim().replace(/\s+/g, ' ').split('');
function reverse(l, r) {
while (l < r) { [chars[l], chars[r]] = [chars[r], chars[l]]; l++; r--; }
}
reverse(0, chars.length - 1);
let start = 0;
for (let i = 0; i <= chars.length; i++) {
if (i === chars.length || chars[i] === ' ') {
reverse(start, i - 1);
start = i + 1;
}
}
return chars.join('');
}Tradeoff: The 'true' in-place version. In JavaScript strings are immutable so we still allocate, but the algorithm is what Microsoft is grading — describe it as the C/C++ in-place algorithm and they're happy.
Microsoft-specific tips
Microsoft will ALWAYS ask 'can you do this in-place?' as a follow-up. Lead with the clean split-reverse-join answer to confirm correctness, then immediately offer 'in C this would be reverse the whole buffer then reverse each word, O(1) extra space.' Saying that sentence unprompted earns the optimal-solution bonus.
Common mistakes
- Forgetting to trim leading/trailing spaces in the output.
- Forgetting to collapse multiple internal spaces.
- Using s.split(' ') instead of s.split(/\s+/) — the former returns empty strings for collapsed spaces.
Follow-up questions
An interviewer at Microsoft may pivot to one of these next:
- Reverse Words in a String II (LC 186) — same problem with a char array as input, asking for in-place.
- Reverse Words in a String III (LC 557) — reverse each word in place, don't change word order.
- What if you had to do this with arbitrary Unicode (combining characters, surrogates)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does Microsoft like the reverse-then-reverse-each-word trick?
It generalizes to other in-place rotation problems and shows you understand that two reversals compose to a rotation of word order. The same idea solves array rotation in O(1) space.
Is split+reverse+join actually accepted?
Yes for correctness, but the in-place follow-up is almost guaranteed. Write the clean version first, then sketch the in-place version unprompted.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Reverse Words in a String and other Microsoft interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →