Magicsheet logo

Reverse Words in a String III

Easy
58.8%
Updated 6/1/2025

Reverse Words in a String III

What is this problem about?

The Reverse Words in a String III interview question asks you to reverse the characters of each individual word in a sentence while preserving the word order and the spaces between them. Unlike the previous versions, the word order stays the same — only the characters within each word are reversed. Leading and trailing spaces are not present in this variant.

Why is this asked in interviews?

This problem is asked at Microsoft, Salesforce, Amazon, Walmart Labs, Google, and Bloomberg as a simpler counterpart to the full word-reversal problem. It tests per-word string manipulation — a common operation in text processing, data formatting, and display systems. It also introduces the concept of operating on substrings independently, which is a building block for more complex string transformations.

Algorithmic pattern used

The pattern is word-by-word character reversal. Split the string on spaces to get individual words. Reverse each word using [::-1] in Python or a two-pointer helper. Join the reversed words back with a single space. This is O(n) time and O(n) space. In-place on a char array: find each word's boundaries using two pointers and apply reverse within each boundary.

Example explanation

Input: "Let us code today"

Split: ["Let", "us", "code", "today"]. Reverse each word:

  • "Let""teL".
  • "us""su".
  • "code""edoc".
  • "today""yadot".

Join: "teL su edoc yadot".

Compare with Reverse Words in a String: that would give "today code us Let" — completely different operation.

Common mistakes candidates make

  • Reversing the entire string instead of each individual word (confusing this with Reverse Words in a String).
  • Rejoining words with double spaces by not using ' '.join(...) correctly.
  • Not handling single-character words — they reverse to themselves, no issue.
  • Forgetting that the sentence may contain words of varying lengths — your reversal must be length-agnostic.

Interview preparation tip

For the Reverse Words in a String III coding problem, the two-pointer string interview pattern applied word-by-word is the foundation. In Python, ' '.join(word[::-1] for word in s.split()) is a clean one-liner. Interviewers at BNY Mellon and Wissen Technology use this to test fluency with string splitting and Python's slice reversal. Practice the in-place two-pointer approach as well — find each word's start by scanning for non-space, find end by scanning to the next space, then reverse in-place within those bounds.

Similar Questions