Adobe Interview Question
Software Engineer / Developers#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getDecimalValue(char c) {
if ((c >= '0') && (c <= '9')) {
return (c-'0');
} else if ((c >= 'a') && ( c <= 'f')) {
return c-'a'+10;
} else {
return -1;
}
}
/**
* Function converting a hex string to a decimal
*/
long htoi(char* ch) {
long rslt = 0;
int v;
char *p = ch;
while (*p != '\0') {
v = getDecimalValue(*p);
p++;
if (v != -1) {
rslt = rslt*16 + v;
} else {
printf("Error Input: %s", ch);
exit(-1);
}
}
return rslt;
}
int main() {
char * hex = (char *) malloc(10*sizeof(char));
printf("enter the hex value: ");
scanf("%s", hex);
printf("Decimal value: %d \n",htoi(hex));
return 0;
}
Missing Scenarios
1. Handling of Case insensitive symbols
2. Handling of 0x and x prefix
0xFFFF , xFFFF, FFFF are all valid hexadecimal notations.
3. Handling of overflow in case the number of hex symbols are greater than 4
0xFFFFF
int htoi(char *s){
static int mulitiplier = 1;
if(*s == '\0') return 0;
int intermediateResult = (*(s+1)) ? htoi(s+1) : 0;
int result = intermediateResult + multiplier * GetInt(*s);
mulitplier *=16;
return result;
}
int GetInt(char c){
if(c =='x) return 0;
if( c >= 97 && c<= 102) {
return c- 'a' + 10;
} else if(c >= 65 && c<=70){
return c - 'A' + 10;
} else{
return c - 48;
}
throw new InvalidArgumentException();
}
1. Third scenario is still not taken care of.
2. Unnecessary processing for 0x making program slow.
Resolving
1. Third scenario is still not taken care of.
2. Unnecessary processing for 0x making program slow.
char * GetParsedInputString(char *input){
int len = strlen(input);
if( len > 1 && input[0] == '0' && input[1] == 'x'){
input= input + 2;
len -= 2;
} else if(len > 0 && input[0] == 'x'){
input = input + 1;
len -= 1;
}
if(len ==0 || len > 4){
throw new InvalidArgumentException();
}
return input;
}
int htoi(char *s){
s = GetParsedInputString(s);
static int mulitiplier = 1;
if(*s == '\0') return 0;
int intermediateResult = (*(s+1)) ? htoi(s+1) : 0;
int result = intermediateResult + multiplier * GetInt(*s);
mulitplier *=16;
return result;
}
int GetInt(char c){
if( c >= 97 && c<= 102) {
return c- 'a' + 10;
} else if(c >= 65 && c<=70){
return c - 'A' + 10;
} else{
return c - 48;
}
throw new InvalidArgumentException();
}
public static int hexToDecConverter(String hexStr){
int result = 0 ;
final int length = hexStr.length();
for(int i= length -1 ; i>=0 ;--i){
int digit = 0;
char ch = hexStr.charAt(i);
if(ch=='0') continue ;
else if(ch>='1' && ch <='9') digit = ch -'0';
else if(ch>='a' && ch <='f') digit = ch -'a' + 10;
else if(ch>='A' && ch <='F') digit = ch -'A' + 10;
else throw new IllegalArgumentException("Illegal char in string ");
int sixteenPower = 1;
for(int j=1; j< length - i;++j)
sixteenPower*=16;
result += sixteenPower*digit;
}
return result;
}
public static int hexToDecConverter(String hexStr){
int result = 0 ;
final int length = hexStr.length();
for(int i= length -1 ; i>=0 ;--i){
int digit = 0;
char ch = hexStr.charAt(i);
if(ch=='0') continue ;
else if(ch>='1' && ch <='9') digit = ch -'0';
else if(ch>='a' && ch <='f') digit = ch -'a' + 10;
else if(ch>='A' && ch <='F') digit = ch -'A' + 10;
else throw new IllegalArgumentException("Illegal char in string ");
int sixteenPower = 1;
for(int j=i; j > 0; --j) // condition should be this
sixteenPower*=16;
}
result += sixteenPower*digit; // Outside the loop
return result;
}
public static int hexToDecConverter(String hexStr){
int result = 0 ;
final int length = hexStr.length();
for(int i= length -1 ; i>=0 ;--i){
int digit = 0;
char ch = hexStr.charAt(i);
if(ch=='0') continue ;
else if(ch>='1' && ch <='9') digit = ch -'0';
else if(ch>='a' && ch <='f') digit = ch -'a' + 10;
else if(ch>='A' && ch <='F') digit = ch -'A' + 10;
else throw new IllegalArgumentException("Illegal char in string ");
int sixteenPower = 1;
for(int j=i; j > 0; --j) // condition should be this
sixteenPower*=16;
result += sixteenPower*digit; // This is correct, indention made me confused
}
return result;
}
int htoi(char *str)
{
char baseCaps = 'A';
char baseSmall = 'a';
int result = 0;
int len = strlen(str);
for(int i=0;i<len;i++)
{
if( (str[i] >= '0') && (str[i] <= '9'))
{
result += ((int)str[i] - 47) * pow(16, len -i -1);
}
else if(str[i] >= 'A' && str[i] <= 'F')
{
result += ((int)str[i] - (int)baseCaps + 10)* pow(16, len - i -1);
}
else if(str[i] >= 'a' && str[i] <= 'f')
{
result += ((int)str[i] - (int)baseSmall + 10)* pow(16, len - i -1);
}
else
{
printf("Wrong Inputs \n");
return -1;
}
}
return result;
}
It should accept prefix 0x too. this code works only that validations have not been done.
Let me know if there are any bugs.
- Vaishnavi September 21, 2009