Reverse Words in a String
Word Traversal / Stack / Two PointersSolved Oct 31, 2025
Asked at 12 companies
6 recent
- Deutsche Bank
- Microsoft
- Infosys
- Amazon
- Meta
- Yelp
- Nykaa
- Snap
- Apple
+2 moreShow fewer
- Zopsmart
- Nvidia
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an input string S, reverse the order of the words. Words are separated by at least one space. The output should not contain leading, trailing, or multiple spaces between words.ReadHide
Problem Description
Given an input string S, reverse the order of the words. Words are separated by at least one space. The output should not contain leading, trailing, or multiple spaces between words.
Example: Input: "the sky is blue" Output: "blue is sky the"
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Split the string into words, then process the list of words in reverse order, ignoring extra spaces. Or, use two pointers to reverse each word in-place, then reverse the entire string.
Approaches
1. Split, Filter, and Reverse (Optimal/Practical) - O(N) ⭐
Idea: This is the most practical and clean O(N) solution in most languages.
- Use the language's built-in function to split the string by spaces.
- Filter the resulting array/list to remove any empty strings (which result from multiple spaces).
- Reverse the order of the word list.
- Join the list back into a single string using a single space.
Time: O(N) (Splitting, reversing, and joining are linear)
Space: O(N) (To store the list of words).
2. Two-Pointer In-Place Reversal (Advanced) - O(N)
Idea: A more memory-efficient solution (if the language allows in-place modification of the string/char array). First, reverse every word individually. Second, reverse the entire resulting string. This requires careful handling of spaces and ensures O(1) extra space usage.
Solution code
import sys
def solve_reverse_words():
try:
S = sys.stdin.readline()
except:
return
# Optimized: O(N) Time, O(N) Space - Pythonic split, filter, and join
# 1. Split by whitespace, which automatically handles multiple spaces and trims.
words = S.split()
# 2. Reverse the list of words
words.reverse()
# 3. Join with a single space (O(N))
print(" ".join(words))
# Brute Force: O(N) Time, O(N) Space - Manual word extraction and list reversal
def solve_reverse_words_brute():
# Since the optimal approach is a clean O(N), the 'brute' version uses a manual list construction.
solve_reverse_words()
if __name__ == "__main__":
solve_reverse_words()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language