3 ms·
I am commonly asked to reverse a C string. The first time I did that, I wrote out what I regarded as the obvious solution. The owner of the company pointed ou
by MichaelCrawford 12y ago
I am commonly asked to reverse a C string.
The first time I did that, I wrote out what I regarded as the obvious solution. The owner of the company pointed out that I had paid attention to performance. That had me puzzled - while I could see that there are lots of really slow ways to reverse C strings, I found it hard to believe that anyone would provide slow solutions to such a simple problem in a job interview.
I got an offer the very next day.
It was not long after that I discovered that many of that company's other coders were, while not actually clueless, just not very adept. Like they really would be challenged to write a fast C string reverser.
At least I got the fizzbuzz test right. I've only been faced with that challenged just once, but three times now I've had to reverse strings.
- userbinator 12y agoI found it hard to believe that anyone would provide slow solutions to such a simple problem in a job interview. These sorts of overly complex solutions usually indicate someone who doesn't actually know how C strings work/how they're stored in memory. Reversing a string is one of the instances where these weaknesses show up. I've seen various people with degrees in CS who claim to have worked with lots of C do this problem, and it's almost amusing the volume and variety of code they can produce for such a simple problem. What should only involve two array indices or pointers in a loop can turn into something containing numerous malloc()s (often with no free()s), copious copying and moving of data around, recursion (this one isn't so bad), and I've even seen sprintf() make an appearance. However, none of this compares to one individual whose quite memorable solution involved creating a temporary file, writing the string into it, then executing an external command to reverse that file, and reading it back in. His reasoning was "because I don't want to reinvent the wheel." (Related article about std::string in C++: https://news.ycombinator.com/item?id=8704318 https://news.ycombinator.com/item?id=8704318)
- MichaelCrawford 12y agoFor C++ std::string, I might well code up an iterator adapter. :-D A while back I mentioned this on Kuro5hin. "K5" members are reknowned for being total jackasses. Some kuron told me that the correct solution was to call the C standard library function that reverses strings - "strrev()" or some such. His point was that one should never implement code from scratch, if one could use a library call. I pointed out that I was required to implement strrev() myself. My interviewer wasn't asking me to call a library function. This set off a flame war that lasted for hours. This compleat gentleman flatly refused to accept that there was any other correct solution than to call a library routine, despite that it was quite clear to me, when I interviewed, that I was not permited to.