Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

#include "string.h"
#include "stdio.h"

main()
{
char str[]="ac1234e5ABCefabcd";
int slen = strlen(str);
int i;
__int64 shex=0;

//first check whether the string can be converted to INTEGER.

//convert string to integer
for(i=0; i<slen; i++)
{
if(str[i]>='0' && str[i]<='9')
{
shex = shex*16 + (str[i]-'0');
}else if(str[i]>='a' && str[i]<='f')
{
shex = shex*16 + (str[i]-'a'+10);
}else if(str[i]>='A' && str[i]<='F')
{
shex = shex*16 + (str[i]-'A'+10);
}
}

printf("%#llx\n\r", shex); //need to convert double to long

}

issues and questions:
ingore space or not? how to handle '-'?
how to print integer number which is bigger than the max value of __int64

- Bin October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Bin - There is already a thread to handle spaces, negative signs and buffer overflow.

- eyeonu.imtiyaz October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int atoi(String s) {
		
		/* to check null and empty */
		if (s == null || s.length() == 0)
			return -1;
		
		int i = 0, number = 0;
		boolean neg = false, err = false;
		
		/* remove spaces from front and end */
		s = s.trim();
		
		/* check for negative sign */
		if (s.charAt(i) == '-') {
			neg = true;
			i++;
		}
		
		/* form number */
		for (; i < s.length(); i++) {

			/* check for invalid chars in between */
			if (s.charAt(i) < '0' || s.charAt(i) > '9') {
				err = true;
				System.err.println("Invalid string");
				break;
			}
			int digit = s.charAt(i) - '0';
			number = number*10 + digit;
		}
		
		if (err)
			return -1;
		
		else if (neg)
			return -number;
		
		return number;
	}

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

int myatoi(char* s)
{
	int a=0;
	while(*s++==' ');
	bool sign=(*s=='-')?1:0;
	if(sign) s=s+1;
	int size=strlen(s);
    for(int i=0;i<size;i++)
	{   
		if((*(s+i)<='9')&&(*(s+i)>='0'))
		{
		a=a*10+*(s+i)-'0';
		if(a>(1<<31-1)||(a<(-(1<<31))))
		{
			return 0;
		}
		}
		else
		{
			return 0;
		}
	}
	return sign?a:-a;

}

- Anonymous December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

for converting String to Hex
String str="hello";
char[] chars=str.toCharArray();
StringBuffer st=new StringBuffer();
for(int i=0;i<chars.length;i++){
   st.append(Integer.toHexString((int) chars[i]));
}
System.out.println("String to hex is :+"st.toString());

- ajit October 31, 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