You are asked to design an algorithm to reverse the words in a string ('hello world' becomes 'world hello'). Which approach would allow you to do this in-place, without using additional memory?

  • A) Using a stack
  • B) Using an array
  • C) Using a linked list
  • D) Using a queue
To reverse words in a string in-place, you can use a stack data structure. You push individual words onto the stack while iterating through the string and then pop them off to reconstruct the reversed string. This approach doesn't require additional memory. The other options do not naturally support an in-place reversal of words.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *