One of My Interview Questions Today
-
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/2001I 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--; } }
-
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
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
-
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/2001Hi, 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
-
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--; } }
(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--; } }
-
(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--; } }
(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; } }
-
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/2001function = std::reverse( sz, sz + strlen(sz)); Regards, Not Joel or pointer arithmetic interviewer
-
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
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