Coincidentally, I was asked this exact question in a phone interview this week. I didn't have to give exact code, I just walked through the principal of it (loop for each digit, dividing by the radix each time using the leftover specify the character (can be a lookup in an array), possible to use bit shifting in base 2/8/16 situations).
The very same is true for "sort a linked-list", though, and that seems to be a popular one. This type of question isn't normally asked because they actually expect you to do it, but because if you don't know some specific things, the question will expose that.
We had a 2 page tech test and did read it all, but most of the time we could mark it based on the first question. For a VB+SQL job, most of the candidates couldn't write a valid INNER JOIN statement.
Oops. Bye.
I believe it is far harder than one could imagine, even more if you are nervous during the interview.
Why not just a binary search. I remember an article claiming 90% of engineer can't write it without bug.
I personnaly asked simply to write a function foo(c,string) (in any language) such that if the string contain the character c, then it returns true. And even with a question as simple as that I had more error I could have imagined.
char itoa(int i) { int strsize; int tmp; char res; int j;
if (i==0) return "0";
// allocate the right size
strsize=0;
if (i<0) strsize++;
tmp=i>0?i:-i;
while (tmp > 0) {
strsize++;
tmp /= 10;
}
res=(char *)malloc(strsize);
// write the values
j=strsize;
res[j]='\0';
if (i<0) {
i=-i;
res[0]= '-';
}
while (i != 0) {
// printf(".%d(%c)", j, '0'+i%10);
res[--j] = '0' + i%10;
i/=10;
}
return res;
}My inital thought is to do something like (in pseudo code):
str = '';
while (i > 0 ) {
# least significant digit
r = i % 10;
# throw away the least significant digit
i = i / 10;
case
when r = 0 then
str = '0' << str
when r == 1 then
str = '1' << str
... etc
end
}
return str;
Would I pass with that? Is there a better way?1. The standard library C function atoi may be named 'ASCII to integer', but it should convert a numerical string in the platform's encoding to integer. For symmetry, itoa should convert an integer to a platform encoded numeric string, not to an ASCII numerical string.
2. Not all computers use ASCII for C strings. Because of that, '0' need not equal 0x30. For example, in EBCDIC, one has '0' == 0xF0.
Utterly pedantic: IIRC, the C standard guarantees that '0' through '9' are contiguous and in order. If you know better, or aren't sure, use "0123456789"[i] to convert a 0...9 value to the corresponding char.
#include <stdio.h>
int main() {
int integer = -1231232342;
int negative = integer < 0 ? -1 : 1;
integer = integer * negative;
char c[11];
int index = 0;
while(integer != 0) {
int position = integer % 10;
c[index++] = 48 + position;
integer = integer / 10;
}
if(negative == -1)
c[index++] = '-';
int swap = 0;
while(swap < index / 2) {
char t = c[swap];
c[swap] = c[index - swap - 1];
c[index - swap++ - 1] = t;
}
printf("%s\n", c);
return 10;
}