01
How would you reverse words in a sentence in place?
Tap to write answer
0 words | 0 charsPress Enter ↵ to reveal
Your Attempt
0 wordsRefined Model Answer
ReferenceI would first think about splitting the sentence into words, but that would use extra space if I create a new string. To optimize it, I would reverse the whole string first and then reverse each word one by one so the words end up in the correct order. The reason I choose this approach is that it is still O(n) time and can be done in O(1) extra space if I have a mutable buffer like a character array. I would also mention edge cases like multiple spaces, leading and trailing spaces, and an empty string. If the interviewer wants a simpler version, I would explain the same logic step by step before coding it.