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.
  • 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
    Todd Smith
    wrote on last edited by
    #9

    Did they also say it was acceptable for your code to crash on invalid input?

    Todd Smith

    M realJSOPR 2 Replies 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
      PIEBALDconsult
      wrote on last edited by
      #10

      I wouldn't store the "delta": for ( i = len / 2 - 1 ; i >= 0 ; i-- ) -- modified at 17:03 Monday 23rd July, 2007 or

      int i = len / 2 ;
      
      while ( i-- ) ...
      
      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
        Tim Deveaux
        wrote on last edited by
        #11

        This kinda works too, without the extra char,but I don't think I'd want it production :)

        char buf\[\] = "swap me you fool!!";
        
        int len = strlen(buf)-1;
        int j=0;
        while(len > j) {
        	\*(buf+j) -= \*(buf+len);
        	\*(buf+len) += \*(buf+j);
        	\*(buf+j) = \*(buf+len)-\*(buf+j);
        	++j;
        	--len;
        }
        
        N 1 Reply Last reply
        0
        • T Todd Smith

          Did they also say it was acceptable for your code to crash on invalid input?

          Todd Smith

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #12

          They should have asked for that in the spec! :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • D Dan Neely

            John Simmons / outlaw programmer wrote:

            (which seemed odd to me).

            likewise. Unless he was counting Nish's implementation as sufficiently different (I wouldn't), I can't think of any other way to do it without using a temp string.

            -- 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

            R Offline
            R Offline
            Robert Surtees
            wrote on last edited by
            #13

            dan neely wrote:

            I can't think of any other way to do it without using a temp string.

            This would be different flip( s, s + strlen( s ) - 1 ); flip( char *h, char *t ) { char temp; if( h >= t ) return; temp = *h; *h = *t; *t = temp; flip( h+1, t-1 ); }

            D N 2 Replies Last reply
            0
            • R Robert Surtees

              dan neely wrote:

              I can't think of any other way to do it without using a temp string.

              This would be different flip( s, s + strlen( s ) - 1 ); flip( char *h, char *t ) { char temp; if( h >= t ) return; temp = *h; *h = *t; *t = temp; flip( h+1, t-1 ); }

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #14

              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 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
                The Wizard of Doze
                wrote on last edited by
                #15

                John Simmons / outlaw programmer wrote:

                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.

                You apply for C jobs? :~ :confused:

                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

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #16

                  Just don't pass that code a DBCS or UTF-8 string!

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                  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

                    P Offline
                    P Offline
                    Phil Harding
                    wrote on last edited by
                    #17

                    Spooky, couple of years back I got asked the same question, gave the exact same answer, interviewer also says he's not seen it done that way before :cool:


                    - "I'm not lying, I'm just writing fiction with my mouth"

                    Phil Harding.
                    myBlog [^] | mySite [^]

                    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

                      C Offline
                      C Offline
                      cmk
                      wrote on last edited by
                      #18

                      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 1 Reply Last reply
                      0
                      • T Tim Deveaux

                        This kinda works too, without the extra char,but I don't think I'd want it production :)

                        char buf\[\] = "swap me you fool!!";
                        
                        int len = strlen(buf)-1;
                        int j=0;
                        while(len > j) {
                        	\*(buf+j) -= \*(buf+len);
                        	\*(buf+len) += \*(buf+j);
                        	\*(buf+j) = \*(buf+len)-\*(buf+j);
                        	++j;
                        	--len;
                        }
                        
                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #19

                        Tim Deveaux wrote:

                        This kinda works too, without the extra char,but I don't think I'd want it production

                        Now if you could rename buf to l1 and len to ll and j to l11, it would be much better!

                        Regards, Nish


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

                        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

                          P Offline
                          P Offline
                          peterchen
                          wrote on last edited by
                          #20

                          The additional loop variable would land on the stack (if it doesn't remain in a secondary index register to begin with). When RAM is really tight - e.g. in micro controllers - stack is usually tuned to the worst case and so is usually less of an issue. But if you have to haggle for a local variable, you usually have to fine-tune your C code anyway.


                          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                          My first real C# project | Linkify!|FoldWithUs! | sighist

                          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

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

                            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

                            realJSOPR P 2 Replies 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

                              S Offline
                              S Offline
                              Stephen Hewitt
                              wrote on last edited by
                              #22

                              The STL way: ----------------------

                              // CommandLine.cpp : Defines the entry point for the console application.
                              //
                               
                              #include "StdAfx.h"
                              #include <iostream>
                              #include <algorithm>
                               
                              void main()
                              {
                              char reverse_me[] = "!desrever neeb evah I";
                              char *pEnd = reverse_me + sizeof(reverse_me)-1;
                               
                              using namespace std;
                              reverse(reverse_me, pEnd);
                              cout << reverse_me << endl;
                              }

                              My point is that perhaps it's a good idea to use standard STL algorithms if possible. A programmer should know how to roll their own but they should also know what's in their language's libraries. I probably would have given two versions: one like this to prove I know STL and a home-spun one to prove I'm not a nitwit.

                              Steve

                              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
                                Pierre Leclercq
                                wrote on last edited by
                                #23

                                I am surprised they even asked you such a kind of question. They should have gone to CP to see this was pointless... :) I also happened to face a surprised interviewer who hadn't seen the solution I was proposing before. Sounds like when your resume starts to be long enough they should think of a "never seen before" way of interviewing those hard-core coders... :) :) :) (Odd is the word)

                                1 Reply Last reply
                                0
                                • R Robert Surtees

                                  dan neely wrote:

                                  I can't think of any other way to do it without using a temp string.

                                  This would be different flip( s, s + strlen( s ) - 1 ); flip( char *h, char *t ) { char temp; if( h >= t ) return; temp = *h; *h = *t; *t = temp; flip( h+1, t-1 ); }

                                  N Offline
                                  N Offline
                                  NormDroid
                                  wrote on last edited by
                                  #24

                                  Interesting use of recursion.

                                  Roger Irrelevant "he's completely hatstand"

                                  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

                                    J Offline
                                    J Offline
                                    jon 80
                                    wrote on last edited by
                                    #25

                                    It's interesting that you swapped characters around; however VS2005 was crashing on me when I was walking through it with the debugger. Unhandled exception at 0x00412e00 in TestConsole4.exe: 0xC0000005: Access violation writing location 0x004156a4. It seems like the pointer is expected to be declared in a different manner with VS2005.

                                    Jon

                                    realJSOPR 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

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

                                      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 P 2 Replies Last reply
                                      0
                                      • T Todd Smith

                                        Did they also say it was acceptable for your code to crash on invalid input?

                                        Todd Smith

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

                                        They just wanted the string reversed. They didn't ask for validation code. :)

                                        "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
                                        • T The Wizard of Doze

                                          John Simmons / outlaw programmer wrote:

                                          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.

                                          You apply for C jobs? :~ :confused:

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

                                          Ummmm, I apply for any job I'm qualified for (which would include C), but in this case (and curiously enough) it's not even a job that requires C++.

                                          "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
                                          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