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



David Tiktin wrote:
> On 29 Sep 2005, "Jaspreet" <jsingh.oberoi@xxxxxxxxx> wrote:
>
> > 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".
> >
> > I told the interviewer that we could keep each word in a node in a
> > linked list and then try to reverse the linked list. That is:
> >
> > --Original linked list
> > Welcome -> to -> google -> groups
> > --After reversing
> > groups -> google -> to -> Welcome
> >
> > I guess the interviewer had learnt/used something else. He wanted
> > me to try reversing it inline that is without the option of using
> > another linked list. He probably wanted me to do something similar
> > to - First reverse the whole string and then individually reverse
> > the words
> >
> > a) Reverse the string to get:
> > spuorg elgoog ot emocleW
> >
> > b) then reverse the individual words to get:
> > groups google to Welcome.
> >
> > 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 telling the interviewer that the solution he thinks of
> > is slightly slower and in-efficient to code or go through by
> > future programmers which I am sure he did not like.
> >
> > I would like an opinion on this from you. Which one is a better
> > and a more efficient solution ?
>
> The 2nd version is the cannonical "cute" solution for interviews, but
> I guess it depends on what primitives you have available. In Perl,
> you're solution would be:
>
> $string = join(' ', reverse(split(/ /, $string)));
>
> I can't think of a way to do the cannonical solution in Perl that's
> as clear and simple. (Maybe someone else can.) In C, maybe the 2nd
> would be easier, unless, of course, you have libraries to do
> essentially what split(), join() and reverse() do (which, as it
> happens, I do ;-).
>
> Dave
>
> --

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.

.



Relevant Pages

  • Re: Reverse words in a string (Another Interview question)
    ... > I was asked to reverse the words in a string. ... > linked list and then try to reverse the linked list. ... > I guess the interviewer had learnt/used something else. ... which they would appear relative to other sprites). ...
    (comp.programming)
  • Re: Reverse words in a string (Another Interview question)
    ... >> I'm using a linked list to contain words that will be output in reverse ... A stack is exactly that. ... C's string handling sucks. ... > void reverse_string(char * ostr) ...
    (comp.programming)
  • Re: EXCEL: How to scan text reversed (like ACCESS: InStrRev)?
    ... InStrRev function. ... You could use a UDF to reverse the string, at least in later versions ... Function Reverse(str As String) As String ... you could use a UDF implementing Regular Expressions ...
    (microsoft.public.excel.worksheet.functions)
  • Re: A function of reverse complement
    ... I see how you reverse the strand. ... I do not understand how you want to complement the strand. ... Function Rev(Forw As String) As String ... >> Dim strReverseSeq As String ...
    (microsoft.public.excel.programming)
  • Re: A function of reverse complement
    ... I see how you reverse the strand. ... I do not understand how you want to complement the strand. ... Function Rev(Forw As String) As String ... >> Dim strReverseSeq As String ...
    (microsoft.public.excel.programming)