Re: Reverse words in a string (Another Interview question)



On 29 Sep 2005 09:36:21 -0700
"Jaspreet" <jsingh.oberoi@xxxxxxxxx> 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/
.



Relevant Pages

  • Re: Free Will
    ... memory and with a hypothetical 'grandmother cell'? ... If past and future were nodes configured as reverse ... what about memory?, how could a net be arranged so as ... before they have run their full course in your mind to ...
    (comp.ai.philosophy)
  • Re: Free Will
    ... memory and with a hypothetical 'grandmother cell'? ... If past and future were nodes configured as reverse ... what about memory?, how could a net be arranged so as ... before they have run their full course in your mind to ...
    (comp.ai.philosophy)
  • Re: Book-able view of ID as speculative science
    ... ever-increasing demand for memory. ... Now measure the z-axis spin of an electron. ... Reverse the x-axis measurement. ...
    (talk.origins)
  • Re: [PATCH] [8/8] RFC: Fix some EFI problems
    ... The test for _WB memory was reversed so it would set cache able memory ... I appreciate that you noticed the reverse logic, ... I pulled this out as it is a real fix. ... The reordering/optimizing needs to be a separate patch. ...
    (Linux-Kernel)
  • Re: reading a file in reverse order (bootom-top)
    ... >> memory you think is large enough for the entire file, ... /* reverse string in place. ... size_t lgh; ...
    (comp.lang.c)