Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. One of My Interview Questions Today

One of My Interview Questions Today

Scheduled Pinned Locked Moved The Lounge
career
47 Posts 32 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C cmk

    That's the way i've usually seen it done, i use something like:

    bool strrev_ip( char *s )
    {
    if( !s ) return(false);

    char *e = s + strlen(s)-1;

    for( ; s < e; s++, e-- ) {
    *s ^= *e;
    *e ^= *s;
    *s ^= *e;
    }

    return(true);
    }

    [EDIT] ... ummm, post order is screwed up, this was in reply to Nish ... and Dan didn't reply to this [/EDIT]

    ...cmk Save the whales - collect the whole set

    N Offline
    N Offline
    nsoneja
    wrote on last edited by
    #31

    cmk wrote:

    That's the way i've usually seen it done, i use something like: bool strrev_ip( char *s ){ if( !s ) return(false); char *e = s + strlen(s)-1; for( ; s < e; s++, e-- ) { *s ^= *e; *e ^= *s; *s ^= *e; } return(true);}

    Nice touch adding the xor way of swapping variables...that's usually a separate interview question in its own right! Also, precludes using a temp variable, which could come across as a loophole (ie; instead of allocating another string)...Hmm can't imagine how they would do it vastly differently than whats outlined in the original posting, all seem to be some variation of swapping chars till the pointers cross over

    1 Reply Last reply
    0
    • N Nish Nishant

      John Simmons / outlaw programmer wrote:

      but the interviewer told me I'm the only person he's seen do it that way (which seemed odd to me).

      I believe it's more common to have a char* to firstPos and a char* to lastPost, and then increment firstPos while decrementing lastPos. You basically did the same except that you avoided pointers and used the array index directly. Depending on his attitude he could take that in a good way (you know how to simplify algorithms and avoid unwanted pointer complexity) but he could also take it in a bad way (as in you were nervous about using pointers directly). Good luck anyhow. :)

      Regards, Nish


      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
      My latest book : C++/CLI in Action / Amazon.com link

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #32

      The compiler will quite likely turn the array indexing into pointer arithmetic operations anyway. This is quite a simple optimization. I prefer to write array-indexing syntax as I find it less confusing.

      Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      • realJSOPR realJSOP

        Write a function that reverses the contents of a null-terminated string in place. The only limitation is that you can't allocate a new string during any part of the process. This is what I gave them:

        void ReverseString(char* str)
        {
        int len = strlen(str);
        int delta = len / 2;
        char pivot;
        for (int i = 0; i < delta; i++)
        {
        pivot = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = pivot;
        }
        }

        I know, it's not all that impressive, but the interviewer told me I'm the only person he's seen do it that way (which seemed odd to me). BTW, I feel that both of the interviews I did today went well. I fully expect to get offers on from each company.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        E Offline
        E Offline
        ednrgc
        wrote on last edited by
        #33

        My guess is that they were really looking for the recursive version.

        realJSOPR 1 Reply Last reply
        0
        • realJSOPR realJSOP

          Write a function that reverses the contents of a null-terminated string in place. The only limitation is that you can't allocate a new string during any part of the process. This is what I gave them:

          void ReverseString(char* str)
          {
          int len = strlen(str);
          int delta = len / 2;
          char pivot;
          for (int i = 0; i < delta; i++)
          {
          pivot = str[i];
          str[i] = str[len - i - 1];
          str[len - i - 1] = pivot;
          }
          }

          I know, it's not all that impressive, but the interviewer told me I'm the only person he's seen do it that way (which seemed odd to me). BTW, I feel that both of the interviews I did today went well. I fully expect to get offers on from each company.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          T Offline
          T Offline
          Tomz_KV
          wrote on last edited by
          #34

          It does not allow to allocate a string. I am wondering if it is ok to use a CharArray (string.ToCharArray()) and then use the reverse method to do it.

          Tom Z. (PMA)

          1 Reply Last reply
          0
          • E ednrgc

            My guess is that they were really looking for the recursive version.

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #35

            On a long string, you will exhaust available stack space , not to mention the time overhead involved in repeatedly calling the function. Therefore, a recursive function is not an appropriate answer. -- modified at 11:08 Tuesday 24th July, 2007

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            1 Reply Last reply
            0
            • D Dan Neely

              recursive, how WTFworthy. :rolleyes:

              -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

              J Offline
              J Offline
              jrbirdman
              wrote on last edited by
              #36

              Well, duh. Everyone knows that you don't type code anymore! It's keyboarded. Stupid VB programmers. :) Someone who likes it both ways, uh huh!

              1 Reply Last reply
              0
              • realJSOPR realJSOP

                Actually, it's not so much a "can-you-do-it" question as much as it is a "how-would-you-approach-it" question.

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                M Offline
                M Offline
                Michael J Collins
                wrote on last edited by
                #37

                I had to look up null terminated. I suck.. :( Seriously, I've been programming for 8 years (not including the 10,20 basic way back in the day), yet every time I see something like this I wonder what the hell I've really learned in all that time. It's discouraging.

                Michael J. Collins Web Application Programmer

                J 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  Actually, it's not so much a "can-you-do-it" question as much as it is a "how-would-you-approach-it" question.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #38

                  John Simmons / outlaw programmer wrote:

                  it is a "how-would-you-approach-it" question

                  That would make more sense.  To see what kind of thinker one is.

                  "The clue train passed his station without stopping." - John Simmons / outlaw programmer

                  1 Reply Last reply
                  0
                  • M Michael J Collins

                    I had to look up null terminated. I suck.. :( Seriously, I've been programming for 8 years (not including the 10,20 basic way back in the day), yet every time I see something like this I wonder what the hell I've really learned in all that time. It's discouraging.

                    Michael J. Collins Web Application Programmer

                    J Offline
                    J Offline
                    jim_taylor
                    wrote on last edited by
                    #39

                    Michael, Back in ancient times (ca.1966) my friend Richard Pratt and I had a little contest, coding a binary search in IBM 360 assembler. At the end we were fighting to remove individual cycles from the loop. When we agreed he had won, reaching the irreducible minimum loop time, Dick Giering showed up with a faster method in hand. His algorithm wasted "lots" of time at the start building a halving table. Once he had the table built, his search ran almost twice as fast as our best effort, because he didn't have a divide instruction inside the loop. Don't feel bad. As the saying goes, "Ve get too soon oldt, und too late schmart." JimT

                    1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      Memory must have been an issue. Otherwise, the requirement would not have been stated the way it was. I could have knocked two more cycles off the cpu load, but it would have cost them 4 more bytes of memory.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      R Offline
                      R Offline
                      rnot
                      wrote on last edited by
                      #40

                      An interesting variation I was faced with in an interview last year was this: Reverse the order of the words in a string in place. You get one character of memory and can assign pointers.

                      1 Reply Last reply
                      0
                      • realJSOPR realJSOP

                        Write a function that reverses the contents of a null-terminated string in place. The only limitation is that you can't allocate a new string during any part of the process. This is what I gave them:

                        void ReverseString(char* str)
                        {
                        int len = strlen(str);
                        int delta = len / 2;
                        char pivot;
                        for (int i = 0; i < delta; i++)
                        {
                        pivot = str[i];
                        str[i] = str[len - i - 1];
                        str[len - i - 1] = pivot;
                        }
                        }

                        I know, it's not all that impressive, but the interviewer told me I'm the only person he's seen do it that way (which seemed odd to me). BTW, I feel that both of the interviews I did today went well. I fully expect to get offers on from each company.

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        C Offline
                        C Offline
                        clydecraig
                        wrote on last edited by
                        #41

                        I don't have a c++ compiler on hand to prove it, but I think this code does the trick for single-byte character sets: void ReverseString(char* str) { char* end = strrchr(str, 0); while(str < --end) { str ^= end; end ^= str; str++ ^= end--; } }

                        C 1 Reply Last reply
                        0
                        • P Paul Conrad

                          John Simmons / outlaw programmer wrote:

                          Memory must have been an issue. Otherwise, the requirement would not have been stated the way it was.

                          Maybe they just wanted to have you think about a solution from a different perspective?  Not sure why they would have a seasoned programmer like yourself write code example to prove yourself.

                          "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

                          P Offline
                          P Offline
                          Peter Mulholland
                          wrote on last edited by
                          #42

                          Joel on Software suggested reversing a string as an interview question as an article some time ago. I got the same question in an interview almost 12 months ago and I think more developers are using it in interviews based on the Joel on Software article. He also gave some explanation of the conclusions you can draw from the solution given by the interviewee. http://www.joelonsoftware.com/articles/fog0000000073.html -- modified at 5:39 Wednesday 25th July, 2007

                          P 1 Reply Last reply
                          0
                          • realJSOPR realJSOP

                            Write a function that reverses the contents of a null-terminated string in place. The only limitation is that you can't allocate a new string during any part of the process. This is what I gave them:

                            void ReverseString(char* str)
                            {
                            int len = strlen(str);
                            int delta = len / 2;
                            char pivot;
                            for (int i = 0; i < delta; i++)
                            {
                            pivot = str[i];
                            str[i] = str[len - i - 1];
                            str[len - i - 1] = pivot;
                            }
                            }

                            I know, it's not all that impressive, but the interviewer told me I'm the only person he's seen do it that way (which seemed odd to me). BTW, I feel that both of the interviews I did today went well. I fully expect to get offers on from each company.

                            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                            -----
                            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                            P Offline
                            P Offline
                            prashant_jalasutrram
                            wrote on last edited by
                            #43

                            Hi, Very Good question and i having 3 1/2 years experience also could not get a clue.Really felt guilty. Thanks for sharing this Q&A. Thanks Prashant

                            prashant_vijaywada

                            1 Reply Last reply
                            0
                            • C clydecraig

                              I don't have a c++ compiler on hand to prove it, but I think this code does the trick for single-byte character sets: void ReverseString(char* str) { char* end = strrchr(str, 0); while(str < --end) { str ^= end; end ^= str; str++ ^= end--; } }

                              C Offline
                              C Offline
                              clydecraig
                              wrote on last edited by
                              #44

                              (amended from previous post) I don't have a c++ compiler on hand to prove it, but I think this code does the trick for single-byte character sets: void ReverseString(char* str) { char* end = strrchr(str, 0); while(str < --end) { *str ^= *end; *end ^= *str; *str++ ^= *end--; } }

                              C 1 Reply Last reply
                              0
                              • C clydecraig

                                (amended from previous post) I don't have a c++ compiler on hand to prove it, but I think this code does the trick for single-byte character sets: void ReverseString(char* str) { char* end = strrchr(str, 0); while(str < --end) { *str ^= *end; *end ^= *str; *str++ ^= *end--; } }

                                C Offline
                                C Offline
                                clydecraig
                                wrote on last edited by
                                #45

                                (amended from previous post -- again) I don't have a c++ compiler on hand to prove it, but I think this code does the trick for single-byte character sets: void ReverseString(char* str) { char* end = strrchr(str, 0); while(str < --end) { *str ^= *end; *end ^= *str; *str++ ^= *end; } }

                                1 Reply Last reply
                                0
                                • realJSOPR realJSOP

                                  Write a function that reverses the contents of a null-terminated string in place. The only limitation is that you can't allocate a new string during any part of the process. This is what I gave them:

                                  void ReverseString(char* str)
                                  {
                                  int len = strlen(str);
                                  int delta = len / 2;
                                  char pivot;
                                  for (int i = 0; i < delta; i++)
                                  {
                                  pivot = str[i];
                                  str[i] = str[len - i - 1];
                                  str[len - i - 1] = pivot;
                                  }
                                  }

                                  I know, it's not all that impressive, but the interviewer told me I'm the only person he's seen do it that way (which seemed odd to me). BTW, I feel that both of the interviews I did today went well. I fully expect to get offers on from each company.

                                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                  -----
                                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                                  U Offline
                                  U Offline
                                  User of Users Group
                                  wrote on last edited by
                                  #46

                                  function = std::reverse( sz, sz + strlen(sz)); Regards, Not Joel or pointer arithmetic interviewer

                                  1 Reply Last reply
                                  0
                                  • P Peter Mulholland

                                    Joel on Software suggested reversing a string as an interview question as an article some time ago. I got the same question in an interview almost 12 months ago and I think more developers are using it in interviews based on the Joel on Software article. He also gave some explanation of the conclusions you can draw from the solution given by the interviewee. http://www.joelonsoftware.com/articles/fog0000000073.html -- modified at 5:39 Wednesday 25th July, 2007

                                    P Offline
                                    P Offline
                                    Paul Conrad
                                    wrote on last edited by
                                    #47

                                    Like John had said, it was more about how to think out the problem rather than can you do it or not.

                                    "The clue train passed his station without stopping." - John Simmons / outlaw programmer

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups