Amazon Interview Question for Software Engineer / Developers






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

// to-do: 1.take the first char in str for signed/unsigned check
// 2.check for invalid char in str
int my_atoi(const char *str)
{
int i=0;
while ( *str )
{
i = (i<<3) + (i<<1) + ((*str) - '0');
++str;
}

return i;
}

- yokuki February 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u explain a bit more ?

what does the following do?

i = (i<<3) + (i<<1) + ((*str) - '0');

- anonymous July 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(x<<3)+(x<<1) is equivalent of "x*10", i.e. (x*8)+(x*2)

- sergey.a.kabanov January 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks Yokuki,

This is one of the most awesome tricks I have ever learned! It made my day.

For those wondering how it works:

You have string "364"

Taking chars from left (one by one):

Example: '3'

('3' - '0') - This gives the actual number in the char.

So i becomes 3. (initially i was 0, therefore bit manipulations resulted in 0)

Then we have to somehow make i from 3 to 30 to left shift the number.

Basically, we want to multiply the number by 10.
(i<<3) + (i<<1) does exactly that.

(i<<3) is 8x
(i<<1)is 2x

(i<<3) + (i<<1) becomes 10x.

Well, I guess now its clear to every one.

- bits July 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int myatoi(char* input)
{
int retVal = 0;
int length = 0;
while(true)
{
if(input[length] != '\0')
{
int val = input[length] - '0';
retVal = retVal*10 + val;
length++;
}
else
{
break;
}
}
return retVal;
}

- Nitin Bhatt December 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Twist: Handle negative numbers and out of range case

int atoi( char* source )
{
	int ret = 0;
	int negative = 1;
	char* i = source;
	if( *i == '-' ){
		negative = -1;
		i++;
	}
	for( ; *i; i++ )
	{
		if( *source >= '0' && *source <= '9' ){
			int temp = ret*10 + (negative)*(*source-'0');
			if( temp < MININT || temp > MAXINT )
				return 0; // or whatever is your out of range handler
			else
				ret = temp;
		else
			return ret;
	}
	return ret;
}

- y2km11 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define MIN_INT -2147483648
#define MAX_INT 2147483647

int my_atoi(const char str[])
{
if(str == NULL) return 0;

int len = strlen(str);

if(len <= 0) return 0;

int index = 0;

//skip leading spaces
while(str[index] == ' ') index++;

bool isNeg = str[index] == '-';
int outNum = 0;

if(isNeg)
{
index++;
// skip white space after the sign
while(str[index] == ' ') index++;
}

while(index < len)
{
char currentChar = str[index++];
if(currentChar >= '0' && currentChar <= '9')
{
int oldValue = outNum;
int charVal = currentChar - '0';
outNum *= 10;
outNum += charVal;

//overflow underflow detection
if(outNum < oldValue)
{
if(isNeg)
outNum = MIN_INT;
else
outNum = MAX_INT;
return outNum;
}
}
else
break;
}
if(isNeg)
outNum = outNum * -1;
return outNum;
}

- atul gupta August 11, 2012 | 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