(a) can they code and TEST int atoi(char *)?
(b) Are they a good cultural fit (ie they like the startup environment)
(c) Then to be sure, I start giving them some real-world challenges we're having with a product and see if they can help.
But (a) eliminates most candidates before I even have to go any further, and if they can do (a) they're generally a good hire. It's kind of bizarre.
I hear you. These basic functions make excellent questions.
You're basically testing to see if people know about casts, right? I'm not a big C developer and most of the languages I spend most of my time in have much less onerous typing systems than C, but if you gave me this question I would probably just write something like:
return (int) char;
perhaps including a loop to read to the end of char*. Would I pass or not? I don't think I'm that bad of a programmer, just don't have a lot of direct experience with C.2.) C isn't required. The functions use the C conventions for naming. That's all.
3.) That solution would never work because it would return the ASCII value for the character.
The solution requires that you go through the string multiplying the value you get by the next power of ten and adding it to what you had before. You work from the 1 position back to the front of the string. You also need to ignore non numerical characters.
That said, I don't really understand why this should be a consideration for the programmer at all. C's type system seems to get in the way and slow things down almost as much as it helps (again, limited C experience here) and I don't think that's how these things should work.
You can work from the front of the string to the end and multiply your accumulated result by 10 at each step, can't you?
I might just be misunderstanding your wording, in which case ignore me.
Your method means you only need to walk the string once which is better. Either way 9/10 interview candidates can't even get that far.
I guess this must be soviet russia.
Then you risk rejecting exceptional candidates who didn't come up with the answer that you learned.
char* itoa(int value, char* str, int base);
char* itoa(int i, void* (*allocator)(size_t));
int itoa(unsigned input, char* buffer, size_t buffer_length);
How a non-programmer could recognize the body of the function is beyond me.The simplest version of the function is one that takes an integer and returns a string. This isn't a trick question and it's not difficult to answer or judge the answer if you take the time to learn it. Assuming that you're going to be hiring a programmer without one handy to help you then you need to read a few books and learn a few things. It's not that hard if you're sufficiently motivated to learn.
There's way way more than "one or two" ways to solve it if a non-programmer is judging. Non-programmers won't have a command of the language necessary to parse uncommon syntax and idioms.
Besides the people who this question would screen could not tell you what an allocator might even be.
However there are a thousand equivalent questions that you could be asked. Can you memorize them all? How do you find out what they are?
string itoa(int i)
{
//convert integer to its string representation
}
In my experience this will tell you enough about the person's proficiency to make a decision. I've seen so many people fall apart on this question in so many different ways it's not even funny. The ones who get it have all been pretty good hires.