Reverse Words in a String
A medium Strings interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Strings
- Sheets
- 3
- Core for
- 14 roles
The problem
Given a string of words separated by spaces, reverse the order of the words and remove extra spaces.
Example 1
- Input
- "code every day"
- Output
- "day every code"
Example 2
- Input
- " hello world "
- Output
- "world hello"
Example 3
- Input
- "keep going strong"
- Output
- "strong going keep"
Constraints
- 1 <= s.length <= 10^4
- s contains English letters and spaces
How to think about it
Updated 2026-09-29Reversing the whole sequence inverts the relative order of both words and characters. Reversing each individual word afterwards flips its characters back to normal while leaving the reversed word order intact. Clean up whitespace along the way so words remain separated by single spaces without leading or trailing padding.
Approaches, worst first
Tokenize and reverse array
time O(n) · space O(n)
Split the string by spaces, discard empty elements produced by multiple consecutive spaces, reverse the resulting list of words, and join them with single spaces. Simple to express, but materializes intermediate lists and copies substrings.
Two-pointer reverse in placeWrite this one
time O(n) · space O(n)
Clean leading, trailing, and multiple internal spaces first. Reverse the trimmed character buffer completely, then iterate through the buffer reversing each space-delimited word individually back to its natural letter orientation.
Where people lose marks · 3
- Failing to trim leading or trailing spaces, resulting in leading or trailing spaces in the final result.
- Multiple consecutive spaces collapsing incorrectly or leaving extra spaces between adjacent words.
- Strings consisting entirely of spaces must collapse cleanly to an empty string.
The theory behind it
Strings — the ground this problem stands on. All Strings problems
What Strings is
A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.
When to reach for it
Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.
How the pattern works
Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.
More Strings problems
Target Roles
Core requirement for 14 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .