X Interview Question

Reverse all the words in a given sentence

Interview Answers

Anonymous

Jan 11, 2013

Iterate through the sentence and push characters onto a stack until you hit a non-alphanumeric character. When that happens pop all values off the stack (barring it's not empty) - printing each as you go - and then print the non-alphanumeric character. Do this until you hit the end of the string. If you hit the end of the string and there are characters in the pop and print each until the stack is empty. O(n) time/space.

2

Anonymous

Jan 11, 2013

*characters in the stack