4 ms·
I TOTALLY agree and I've never heard anyone say the same thing. My interviews have all been boiled down to: (a) can they code and TEST int atoi(char *)? (b) A
by rubyrescue 16y ago
I TOTALLY agree and I've never heard anyone say the same thing. My interviews have all been boiled down to:
(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.
- jswinghammer 16y agoAtoi is even easier than itoa :) I hear you. These basic functions make excellent questions.
- chrisaycock 16y agoYeah, I start asking them to code atoi(). That question works miracles.
- cookiecaper 16y agoThe glibc implementation of atoi basically just offloads to strtol, which just offloads to strtol_l.c, which is pretty complex at first glance. 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.
- jswinghammer 16y ago1) In this scenario you are implementing that mechanism. Casts don't work via magic. 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.
- spacemanaki 16y ago>You work from the 1 position back to the front of the string. 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.
- jswinghammer 16y agoI think that might work. My method makes more sense to me but that's because it's just the first thing that came to mind when thinking about the problem. 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.
- BillMartin 16y agoYou're forgetting to -0x20 or -'0'. I guess this must be soviet russia.
- cookiecaper 16y agoCan I see a functional implementation just for educational purposes here? I don't understand really because I have casted things in C like that before (not in lieu of atoi() with (int) char as shown, but other datatypes) and because in my experience, when I have a read out a string, I have not gotten an ASCII keycode, but the inputted character. Most of my C experience has been on existing open-source projects, so maybe they'd already implemented stuff that glossed over all this. 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.
- drtse4 16y agoCastings in c doesn't perform type conversion, regarding the implementation just consider that every ascii char has a numerical code (it's int value) and that in the ascii table the numbers are adjacent.
- rubyrescue 16y agoas jswinghammer says you have to implement the mechanism that does this. when you do, don't forget negatives, trailing and leading spaces, and non-base-10 numbers (last one is not a requirement).