Amazon Interview Question for Software Engineer / Developers






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

very easy :)
int atoi(char *n)
{
int num;
while(*n!='\0'&&(*n>='0' && *n<='9'))
{num=num*10+*n-'0';
n++;
}
return num;

}

- geeks July 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not perfect!
1) you want to consider negative number
2) you want to consider space before the number or '-'
3) you want to consider overflow of integer
4) you want to consider illegal charactor

- Charles December 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int main()
{
char *str = "1234";
int n;
n=myatoi(str);
printf("%d\n", n);
return 0;
}

myatoi(char *ptr)
{
int val=0;
if(ptr)
{
while(*ptr && (*ptr > '0' || *ptr <'9'))
{
val= (val * 10) + (*ptr - '0');
ptr++;
}
}
return val;
}

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

int atoi(const char* string)
{
    char c;
    int result = 0;
    int sign;
    if(*string == '-')
        {
        sign = -1;
        string++;
    }
    else
        sign = 1;
    while((c = *string++) != '\0')
        result = result*10+(c-'0');
    return result*sign;
}

- Giorgi February 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. You could cause overflow in result = result*10+(c-'0');
2. Check whether each character in the string could be converted to integer

- Bond February 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey57626" class="run-this">main()
{
string str="1234"
int num=0,i;
for(i=0;i<strlen(str);i++)
{

num=(num*10)+(str[i]-'0');
}
}

</pre><pre title="CodeMonkey57626" input="yes">
</pre>

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

///#include <stdio.h>
#include <string.h>

int main()
{
int no;
char *str="-47978757";
no=convert(str);
printf("%d",no);
return 0;
}

int convert(char *str)
{

int k=1;
int i;
int sum=0;

for(i=strlen(str)-1;i>=1;i--)
{

sum+=(str[i]-'0')*k;
k=k*10;

}
if(str[0]!='-')
sum+=(str[0]-'0')*k;
else
sum=sum*(-1);

return sum;
}\\\

- sandeep vasani December 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>

int main()
{
    int no;
    char *str="-47978757";
    no=convert(str);
    printf("%d",no);
    return 0;
}

int convert(char *str)
{
    
    int k=1;
    int i;
    int sum=0;
    
    for(i=strlen(str)-1;i>=1;i--)
    {
        
        sum+=(str[i]-'0')*k;
        k=k*10;
      
    }
    if(str[0]!='-')
    sum+=(str[0]-'0')*k;
    else
    sum=sum*(-1);
    
    return sum;
}

- sandeep vasani December 16, 2014 | 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