Subj : Re: Reverse words in a string (Another Interview question) To : comp.programming From : Steve O'Hara-Smith Date : Thu Sep 29 2005 11:44 pm On 29 Sep 2005 09:36:21 -0700 "Jaspreet" wrote: > Hi All > > Just had another interview with those same questions which hardly make > a sense except this one probably. > > I was asked to reverse the words in a string. Say if we have "Welcome > to google groups", I need to have it reversed to "groups google to > Welcome". Hmm - when I was asked that one there was a big hint that I was to make it as memory efficient as possible. That led me straight to the expected answer of reversing the string and then the words. > Now agreed my solution would take up extra memory but am not sure which > one is a faster and more efficient solution. I did the mistake of As has been pointed out they are both of the same order of execution speed, one is more efficient in use of memory the other is clearer. > I would like an opinion on this from you. Which one is a better and a > more efficient solution ? Which is better depends on the constraints surrounding the problem (eg. if you are in an embedded environment with tight memory constraints then go for the inline approach every time). If the code is running in an environment with plenty of memory and may be maintained by copro grammers or cow orkers then use the most obvious code you can. -- C:>WIN | Directable Mirror Arrays The computer obeys and wins. | A better way to focus the sun You lose and Bill collects. | licences available see | http://www.sohara.org/ .