Skip to main content

86. Reverse Words in a String

mediumAsked at Reddit

Reverse the order of words in a string. Reddit uses this string-manipulation question to test pointer fluency — the same shape used when normalizing search query terms in their suggest service.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit search-team phone screen.

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^4
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '.
  • There is at least one word in s.

Examples

Example 1

Input
s = "the sky is blue"
Output
"blue is sky the"

Example 2

Input
s = "  hello world  "
Output
"world hello"

Example 3

Input
s = "a good   example"
Output
"example good a"

Approaches

1. Split, reverse, join (one-liner)

Split on whitespace, filter empty, reverse, join with single space.

Time
O(n)
Space
O(n)
function reverseWords(s) {
  return s.split(/\s+/).filter(Boolean).reverse().join(' ');
}

Tradeoff: Idiomatic. Built-ins handle whitespace edge cases.

2. Manual two-pointer (optimal for in-place languages)

Reverse whole string, then reverse each word.

Time
O(n)
Space
O(1) (in C++); O(n) in JS due to string immutability
function reverseWords(s) {
  const chars = s.trim().split('');
  // Reverse whole array
  chars.reverse();
  // Reverse each word in-place
  let i = 0;
  while (i < chars.length) {
    while (i < chars.length && chars[i] === ' ') i++;
    let j = i;
    while (j < chars.length && chars[j] !== ' ') j++;
    // chars[i..j) is a word
    for (let l = i, r = j - 1; l < r; l++, r--) [chars[l], chars[r]] = [chars[r], chars[l]];
    i = j;
  }
  // Collapse multiple spaces
  return chars.join('').replace(/\s+/g, ' ');
}

Tradeoff: Verbose but demonstrates the in-place algorithm. In JS, the one-liner is preferred.

Reddit-specific tips

Reddit interviewers will accept the one-liner if you discuss the alternatives. Bonus signal: if asked for in-place, walk through the reverse-whole-then-reverse-each-word trick.

Common mistakes

  • Splitting on single space ' ' instead of /\s+/ (multi-space breaks).
  • Forgetting trim — leading/trailing whitespace creates empty tokens.
  • Returning array instead of string.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Reverse words in string II (LC 186) — in-place on char array.
  • Reverse words in string III (LC 557) — keep word order, reverse each.
  • Length of last word (LC 58).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why /\s+/ for split?

Multiple consecutive spaces produce empty tokens with a single-space split. \s+ collapses them.

How would you do this in O(1) memory?

Requires a mutable string (C/C++). The reverse-whole-then-each-word trick works in-place.

Practice these live with InterviewChamp.AI

Drill Reverse Words in a String and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →