Subj : Re: Reverse words in a string (Another Interview question) To : comp.programming From : Gerry Quinn Date : Sat Oct 01 2005 01:39 pm In article , joe@burgershack.com says... > Gerry Quinn wrote: > > 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. > 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. For me it's an obvious mapping, and perfectly idiomatic. It's not minimalist, but I do not accept minimalism as a valid criterion in general. > 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. I'm using a linked list to contain words that will be output in reverse order (or any other order I choose) after processing, which can be in any order I choose. (There is no reason why processing should be in reverse order just because that is the desired output.) Using the minimal algorithm is like carefully choosing a shopping bag no larger than will be needed for the goods you intend to buy. It is a waste of time that yields no significant benefits and reduces flexibility. - Gerry Quinn .