91. Reverse Each Word and Then Reverse the Word Order
Given the string "How are you", produce two outputs. First, reverse the characters inside each word to get "woH era uoy". Second, reverse the order of the words to get "you are How". Explain the transformations and complexity.
I first split the sentence into a list of words. For the first output, I reverse the characters inside each word while keeping the words in their original positions. For the second output, I reverse the order of the original words without changing their characters. For "How are you", the results are "woH era uoy" and "you are How". The algorithm takes O(n) time and O(n) auxiliary space because it creates word data and new output strings.
See the Code while reading this explanation.
The problem asks us to apply two different transformations to the same input sentence. One transformation reverses the characters inside each word. The other reverses the positions of the words. Splitting the sentence into words is a good fit because it lets us transform each word or change the list order without mixing the two operations.
- What input sizes, value ranges, and edge cases should the solution handle?
- What output should be returned for empty, invalid, or duplicate input?
- Should I prioritize execution time or memory use, and may I use the standard library?
The input is the string "How are you".
We need to produce two separate results.
For the first result, each word stays in its original position, but the characters inside the word are reversed.
"How" becomes "woH".
"are" becomes "era".
"you" becomes "uoy".
The first output is "woH era uoy".
For the second result, each word keeps its original characters, but the order of the words is reversed.
["How", "are", "you"] becomes ["you", "are", "How"].
The second output is "you are How".
We call split on the input string.
Before this step, the state is the string "How are you".
After this step, the state is the list ["How", "are", "you"].
This original word list is used to build both outputs.
We process the words from left to right.
The current word is "How". Reversing its characters gives "woH".
The current word is "are". Reversing its characters gives "era".
The current word is "you". Reversing its characters gives "uoy".
The transformed words are ["woH", "era", "uoy"].
We join them with one space between neighboring words. The first result is "woH era uoy".
We use the original list ["How", "are", "you"]. We do not use the character-reversed words from the first transformation.
Reversing the list order gives ["you", "are", "How"].
We join these words with spaces. The second result is "you are How".
For the first transformation, the algorithm applies character reversal independently to every word. It never changes the position of a word. Therefore, each output word is the reverse of the corresponding input word.
For the second transformation, the algorithm reads the original word list from the last position to the first position. It does not change the characters inside a word. Therefore, the output contains the original words in exactly reversed order.
The function calls text.split() to create the original word list. A generator expression applies word[::-1] to each word, and join builds the first output. The expression words[::-1] creates the words in reversed order, and another join builds the second output. The function returns both strings as a tuple.
Let n be the total number of characters in the input. Splitting the string, reversing all word characters, reversing the word list, and building the output strings together take O(n) time.
The auxiliary space complexity is O(n). The word list, reversed list slice, temporary reversed word strings, and returned strings grow with the input size.
For an empty string, both outputs are empty strings. For a one-word string, the first output reverses that word, while the second output is unchanged. Character case is preserved. Because the implementation uses split and joins with one space, repeated spaces or leading and trailing spaces are normalized in the outputs.
The key insight is that the two required results change different levels of the sentence. The first result changes characters inside each word. The second result changes the positions of whole words. We first create the original word list and then build both results independently from it. The first invariant is that every processed word remains at its original word position and has its characters reversed. The second invariant is that every output position receives the corresponding original word from the opposite end of the list.
def reverse_each_word_and_word_order(text: str) -> tuple[str, str]:
words = text.split()
reversed_each_word = " ".join(word[::-1] for word in words)
reversed_word_order = " ".join(words[::-1])
return reversed_each_word, reversed_word_order
if __name__ == "__main__":
input_text = "How are you"
first_output, second_output = reverse_each_word_and_word_order(input_text)
print(first_output)
print(second_output)Let n be the total number of characters in the input string. Splitting the sentence takes O(n) time. Reversing the characters across all words takes O(n) time because the total number of word characters is at most n. Reversing the word list and joining both outputs also take O(n) time. Therefore, the total time complexity is O(n). The auxiliary space complexity is O(n) because the code creates a word list, a reversed list slice, reversed word strings, and new result strings.
This pattern is useful in text-processing tools that split a sentence into tokens, transform individual tokens, reorder tokens, and rebuild the final text. It also tests common Python string operations such as split, slicing, generator expressions, and join.
The interviewer is checking whether the candidate can distinguish between reversing characters and reversing word positions. The problem also tests correct use of Python string slicing, split, join, generators, and list slicing. A strong candidate keeps both transformations independent, uses the original words for the second output, explains the exact example correctly, and gives an accurate O(n) time and O(n) auxiliary space analysis.
A common mistake is reversing the complete string, which produces "uoy era woH" and combines both transformations incorrectly. Another mistake is using the reversed-character words to build the second output instead of using the original word list. A candidate may also reverse the word order for the first output or reverse the characters for the second output. Another mistake is claiming O(1) auxiliary space even though Python creates new lists, slices, reversed strings, and result strings. It is also easy to forget that split normalizes repeated whitespace.
Show the original word list once, and then draw two separate branches from it. One branch reverses characters inside each word. The other branch reverses only the list order. This makes the difference between the two outputs clear.









