Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
One more
int atoi_loc(char *nums)
{
int trav = 0, sign = 1, numi = 0;
if (nums[trav] == '-')
{
sign = -1 ;
trav++;
}
while (nums[trav] != '\0')
{
if (nums[trav] <= '9' & nums[trav] >= '0')
{
numi = (nums[trav] - '0') + numi * 10 ;
}
else
{
return 0;
}
trav++;
}
return numi * sign;
}
int atoi(const char *str)
{
int i=0;
while ( *str )
{
// i<<3 + i<<1 = i*10, to change decimal place of int
i = (i<<3) + (i<<1) + ((*str) - '0');
++str;
}
return i;
}
@limca500ml :- This question is too easy to qualify as Amazon Interview questions. And if the interviewer ask these easy questions then it means they will show no mercy to your solution. So you have to write perfect code. By perfect, I mean no bug, all testcases handled.
Your solution will fail in following condition :-
1.) If string denotes negative number i.e. "-12345".
2.) If string is in hexadecimal i.e. "0xFF" or "xFF".
3.) If string is in octal i.e. "013".
Some other invalid test cases can be.
1.) If string is like "--12"
2.) If string is like "-12-3"
3.) If string is contains only characters "abcde".
4.) If string is null.
P.S. - Also you can handle integer overflow.
Thanks,
Comments are welcome
I think that to address the hex and oct concerns the function might have parameter "base" that is 10 by default;
this parameter value can be also used for the input string validation.
Why you need extra parameter for base. Its well known that if a number starts with '0x' or 'x', it is hex number and if it starts with '0' it is octal representation of number.
javascript version:
function atoi(str, base){
var sign=1;
if(str[0]=='-'){
sign = -1;
str = str.substring(1);
}
var length = str.length - 1;
if(!base)
base = 10;
var res = 0;
var zeroCode ='0'.charCodeAt(0);
for(var i=length;i>=0;i--){
var factor = length - i;
res+=Math.pow(base, factor) * (str.charCodeAt(i) - zeroCode);
}
return res * sign;
}
The
int atoi(const char *nptr)
function converts the initial portion of the string pointed to by nptr to int.
(From Ubuntu man pages)
Its ascii to Integer funtion. for instance if you provide A it returns you the integer value
How about calling sscanf to solve this problem ?
int atoi(const char* nums)
{
int num = 0;
sscanf(nums, "%d", num);
return num;
}
Here's an implementation of atof, handling -ve & whitespaces:
bool isDigit(char c){
return (c >= '0' && c <= '9');
}
double atof(const char * floatStr) throw(const char *){
if(!floatStr) throw "NULL string exception";
double res = 0;
int sign = 1;
int i = 0;
while(floatStr[i] == ' ')
i++;
if(floatStr[i] == '-'){
sign *= -1;
i++;
}
while(floatStr[i] == ' ')
i++;
for(;floatStr[i] != '.' && floatStr['\0']; i++){
if(!isDigit(floatStr[i]))
throw "Invalid char exception";
res = res*10 + (floatStr[i] - '0');
}
if(floatStr[i] == '.'){
double fracBase = 10;
for(i+= 1; floatStr[i] != '\0'; i++){
if(!isDigit(floatStr[i])){
throw "Invalid char exception";
}
res += (floatStr[i] - '0')/fracBase;
fracBase *= 10;
}
}
if(floatStr[i] == '\0')
return res * sign;
return 0;
}
- Learn Android: http://learnandroideasily.blogspot.in/ February 06, 2013