DSA interview question
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".
string reverseEachWord(string s); // reverses each word's characters; spaces stay in place
Input s | Output | Explanation |
|---|---|---|
"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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in