Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

See The Link  bestinterviewsquestions.blogspot.in/2013/01/convert-string-to-integer-implement-atoi.html

- Learn Android: http://learnandroideasily.blogspot.in/ February 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

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

- Chandrahas January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ascii to integer conversion.

- pradeep1288 January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a neat solution.

- pradeep1288 January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are missing boundary conditions like if the string contains the number > INT_MAX

- avikodak January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How can u deal with the overflow problem ?

- oohtj January 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of using the int .. we have use vector/linked list to store the value

- avikodak January 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

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 January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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

- Nitin Gupta January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- S.Abakumoff January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Nitin Gupta January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you specify the notation you are referring to? is it C notation?

- S.Abakumoff January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about making it correct, like handling negative numbers, before worrying about optimizations. Also, if your optimization is indeed useful on today's processors any good compiler will do if automatically. However, if not usefull, you most likely have slower code. Fail.

- Anonymous January 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- S.Abakumoff January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What's atoi()??? :)

- ME January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The

int atoi(const char *nptr)

function converts the initial portion of the string pointed to by nptr to int.
(From Ubuntu man pages)

- Chandrahas January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its ascii to Integer funtion. for instance if you provide A it returns you the integer value

- Luqman_dev January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So the same thing as Java's charAt?

- SleepyHead January 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about calling sscanf to solve this problem ?

int atoi(const char* nums)
{
   int num = 0; 
   sscanf(nums, "%d", num);
   return num;
}

- oohtj January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will either crash (undefined behavior dereferencing a null-pointer) or always return 0.

You are hired!

- Anonymous January 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- theGhost September 10, 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