Adobe Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

It should accept prefix 0x too. this code works only that validations have not been done.
Let me know if there are any bugs.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void htoi(char *);
int main()
{
  char *hex;
  hex=(char *)malloc(10*sizeof(char));
  printf("enter hex value");
  scanf("%s",hex);
  
   //if(!hex)
     //return 0;
  htoi(hex);
  return 0;
}

void htoi(char *hexstr)
{
 char c;
 int val=0,i;

 int len=strlen(hexstr);
 for(i=0;i<len;i++)
 {
   c=*hexstr;
   printf("char :%c",c);
   if(c>='a' && c<='f')
   {
     val=val*16+(c-'a'+10);
   }
   else if(c>='A' && c<='F')
   {
     val=val*16+(c-'A'+10);
   }
 else
  {
    if(c>='0' && c<='9')
    {
      val=val*16+(c-'0');
    }
  }
  hexstr++;
 }

 printf("Val: %d   ",val);
}

- Vaishnavi September 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- hunt September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Ankush November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
}

- Ankush Bindlish November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int htoi(char *num, bool & is_valid){
  is_valid=true;
  num=toupper(num);
  char * start = num;
  --start;
  while (*num++) ;
  --num;
  int hex=0, i=0;
  while(num != start){
   if(*num <'0' || *num > 'F')
   {is_valid=false; break;}  
   hex += (*num-'0') *pow(16,i);
   ++i;
   --num;
  }
  return hex;
}

- Anonymous November 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think use of expensive pow() is needed. Though my first approach was same ;)

- alchemist December 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;

}

- Anon January 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;

}

- Goert May 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;

}

- Goert May 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can some one explain the logic a little bit ?

- abhimanipal January 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- shahidnitt September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = htoi (str) ;

- POSIX is your friend March 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
unsigned int htoi(char s[]){
unsigned int res = 0, i = 0;
char c = 0;

if(s[i]=='0') i++;
if(s[i]=='x' || s[i]=='X') i++;
for (; isdigit(s[i]) || ((c=tolower(s[i])) && c >='a' && c <='f'); ++i,c=0)
res = 16 * res + (c?(c-'a'+ 10):(s[i] - '0'));

return res;
}
}

- jv January 05, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More