Amazon Interview Question for Software Engineer in Tests






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

read each character and get its numerical equivalent by subracting the ascii value of '0' from the ascii value of the character.
build the number using a loop and multiplying by 10 and adding it to the already formed number.
reverse the number at the end(since we read the number in reverse from the char*)

- bob March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@bob, instead of reversing the number at the end you can also, calculate the number of digits beforehand, and set values by decrementing a pointer.

also, be sure to take into account negative numbers if the interviewer wants.

- woohoo March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

nugarp is correct, scan the char array from left to right, check for the first negative sign, then for the rest of characters, sum = sum * 10 + (array[i] - '0'), negate at the end if the first char is '-'

- airfang613 March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int atoi(char* numberstr){
    int num=0;
    bool sign  =false;
    int length = getLength(numberstr);
    if(numberstr[0]='-'){
        sign = true;
        numberstr++;
        length--;
    }        
    while(*numberstr){
        num = (*numberstr - '0')*(length--);
        numberstr++;
    }
    
    if(sign) num = num*(-1);
    
    return num;
}

The code above reads the number from the string from left to right, setting the sign flag if the first character is '-'.

- qwerty March 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey82292" class="run-this">#include <stdio.h>
#include <ctype.h>
#include <errno.h>

int myatoi(const char *src)
{
int c;
int total;
int sign;
int overflow = 0;

int max = 0x7FFFFFFF / 10;

while(isspace((int)(unsigned char)*src))
++src;
c = (int)(unsigned char)*src++;
sign = c;
if(c == '-' || c == '+')
c = (int)(unsigned char)*src++;
total = 0;
while(isdigit(c))
{
if(total > max) { overflow = 1; break; }
total = 10 * total + (c - '0');
c = (int)(unsigned char)*src++;
}

if(overflow) {
errno = ERANGE;
return 0x7FFFFFFF;
}

if(sign == '-')
return -total;
else
return total;
}

int main() {
char src[100];
while(scanf("%s", src))
printf("%d\n", myatoi(src));
}
</pre><pre title="CodeMonkey82292" input="yes">
</pre>

- Anonymous August 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think this can work too. i have also tested this and this worked. if there is any mistake then please point it out. all constructive comments are welcome.

public static int atoi(String a){
	char[] arr = a.toCharArray();
	int result = 0;
	for(int i=0; i<arr.length; i++){
		int k = (int) arr[i];
		k = k-48;
		result = (result*10)+k;
	}
	return result;
}

- CAFEBABE December 26, 2011 | 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