Subj : Re: Reverse words in a string (Another Interview question) To : comp.programming From : Randy Date : Fri Sep 30 2005 02:51 pm Gerry Quinn wrote: > In article , joe@burgershack.com says... > >>Jaspreet wrote: >>... >> >>>I guess then the interviewer was justified in rejecting me. There goes >>>another job for me. How I wish I should have agreed to the cannonical >>>solution but I thought reversing the string first and then reversing >>>the individual words is going to cost me on time and efficiency. >> >>Maybe he just wanted to know if you knew what a stack was. IMHO, that's >>the obvious data structure to use when reversing a string of input tokens. > > > Surely a linked list is a perfectly good alternative? > > If the interviewer then wants you to do it inline, that is fine, but a > different problem from the one he originally set, which was to reverse > the words. > > - Gerry Quinn A linked list will certainly do the job, but unlike a stack, it will also do other jobs than simple sequence reversal. As such, linked list code is not ideally minimalist or idiomatic -- an obvious natural mapping of problem space to solution space. Choosing the most appropriate data structure is like choosing the right programming language: it's the most direct path to the desired goal. When you do this, other developers don't really even have to read your code to know what you're doing. The design is apparent from the tool you chose; you're using a stack to retain state that will be processed in reverse order. I'm proposing a solution that I think is better, not the only correct one. Randy .