PPractick

DSA interview question

Reverse Every Word in a String

Medium · Algorithms · Topics: string, two pointer

The problem

A kids' puzzle app shows sentences with every word spelled backwards while the words stay in their original order and positions, like "Mr Ding" → "rM gniD", and players guess the sentence. The transformation reverses the characters of each word in place, leaving the spaces exactly where they are. It's a two-pointer string exercise: find each word's boundaries and reverse that range. (Reversing the order of the words, rather than their letters, is S.No 135.)

Example: "Let's take LeetCode contest" → "s'teL ekat edoCteeL tsetnoc".

Function signature

string reverseEachWord(string s);  // reverses each word's characters; spaces stay in place

Test cases

Input sOutputExplanation
"Let's take LeetCode contest""s'teL ekat edoCteeL tsetnoc"Each word reversed, order kept
"Mr Ding""rM gniD"
"a""a"
"ab cd""ba dc"
" hi "" ih "Spaces are preserved exactly

(Confirm: words are separated by spaces; the word order and all spaces stay the same; punctuation belongs to the word.)

How interviewers phrase it

Clarifying questions to ask

  1. Reverse the letters of each word, or the order of the words?
  2. Should spaces be preserved exactly, including repeated spaces?
  3. Is in-place modification allowed?

Follow-ups to expect

  1. "How do you find the words?"
  2. "How does this relate to reversing the word order?"
  3. "What's the extra space?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in