Amazon Interview Question for Software Engineer / Developers


Interview Type: In-Person




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

public long stringToInt(String s) throws Exception
{
	if(s == null) 
		throw new Exception("Null values for entered input");
	else
	{
		int isNegative = 1;
		if(s.charAt(0) == '-')
		{
			s.substring(1);
			isNegative = -1;
		}
		if(s.length() == 0)
			throw new Exception("Unrecognized string");
		else
		{
			long result = 0;
			for(int i = 0; i < s.length(); i++)
			{
				int value = s.charAt(i) - '0';				
				if(value < 0 || value > 9)
					throw new Exception("Unrecognized string");
				result = result * 10;
                                
                                if(result > Integer.MAX_VALUE - value)
                                        throw new Exception("Integer Overflow"); 
				result += value;
			}			
			return isNegative * result;
		}
	}
}

- Dev March 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good it is having all test cases included

- ahmad March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This same code earned me a f2f for google :)

- hello world March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(value < 0 && value > 9)
throw new Exception("Unrecognized string");


Really ??

- Anonymous March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

where is the overflow detection.

- Rayden March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did it when I coded on the paper, but forgot here... there is an interesting way to achieve that if you see, I added that to the code.
Thanks for pointing it out.

Here is the list of test cases I used in the interview
null(null string)
""(Empty String)
-
Integer.MAX_VALUE
Integer.MIN_VALUE
123,234,677
-100
100
02324
12345.9812
12a3

- Dev March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Few good test case would have been:
1. where input contains spaces in the beginning
2. input string containing a + sign in the beginning

Here is a 'C' Code with these two cases accounted as well:

long atoi(char *a)
{
	long num=0;
	int sign=1;
	int i=0;
	
	// Remove any whitespaces that might be there in the beginning
	while(a[i]==' ' || a[i]=='\t')
		i++;
	
	// If sign is there, consider taking it and moving forward
	if(a[i]=='-')
	{
		sign=-1;
		i++;
	}
	else if(a[i]=='+')
		i++;
	
	// Keep taking digits one by one, validate digit and add it to 10*num 
	while(a[i]>='0' && a[i]<='9')
	{
		num = num*10+a[i]-'0';
		i++;
	}
	return num*sign;	
}

- san_me2 March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@hello world -

result = result * 10;
                                
                                if(result > Integer.MAX_VALUE - value)
                                        throw new Exception("Integer Overflow");

Is your Overflow detection correct?

- Anonymous March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is a bug. you only check the overflow when you add. you didn't check it when you multiply. actually this happens more.

so you should check like this:

int UpperBound = Integer.MAX_VALUE / 10;
if(result > UpperBound) throw new Exception("Integer Overflow");
result = result * 10;
//the same

- sunzhaonan January 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
int i,sum=0;
string a="5344";
for(i=0; i<a.length() ; i++)
sum+=(a[i]-'0')* pow(10,a.length()-i-1);
cout<<sum;
system("pause");
return 0;
}

- pavan009 March 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

function(char s[ ])
{
int i,num=0,sign=1;
if(s[0]==' - ')
sign*=-1;

for(i=0;i<strlen(s);i++)
{
num=(num*10)+(s[i]-48);
}
num*=sign;
cout<<num;
}

- awesome_tanmoy March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int _tmain(int argc, _TCHAR* argv[])
{
char *s = "-5344";
bool isValid = true;
int i,num=0,sign=1;
if(s[0]=='-') {
sign*=-1;
i = 1;
} else {
i = 0;
}

for(;i<strlen(s);i++)
{
if( (s[i] == '0') || (s[i] == '1') || (s[i] == '2') || (s[i] == '3') ||
(s[i] == '4') || (s[i] == '5') || (s[i] == '6') || (s[i] == '7') ||
(s[i] == '8') || (s[i] == '9')
) {
num=(num*10)+(s[i]-48);
} else {
printf("Invalid Input\n");
isValid = false;
}

}
num*=sign;

if(isValid == true) {
printf("num = %d\n", num);
}
return 0;
}

- Anonymous March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static int convertStringToInt(String string) throws Exception {
if (string == null) {
throw new Exception("cannot convert null to int");
}

char[] chars = string.toCharArray();
int result = 0;
int j=0;
for (int i=chars.length-1;i>0;i--,j++) {
result += Character.digit(chars[j], 10) * Math.pow(10,i);
}
result += Character.digit(chars[j], 10);

return result;
}

- Angus Ng March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long Convert_number(chat *a,int size)
{
long Number=0;

for(int i=0;i<size;i++);
number= number*10 + (a[i]-'0');

return(number);
}

- Sanjay Kumar March 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int atoiStr(String str) throws Exception {
boolean negative = false;
//is Null
if (str.equals("")) {
throw new Exception("null string!");
}
else
//is Negative
if (str.charAt(0) == '-' || str.charAt(0) == '+') {
if (str.charAt(0) == '-') {
negative = true;
}
}
return processStr(negative, str);
}

public static int processStr(boolean isNeg, String str) throws Exception{
long value = 0;
int begin = isNeg ? 1 : 0;
for (int i = begin; i < str.length(); i++) {
if (str.charAt(i) >= '0' && '9' >= str.charAt(i)) {
value = value * 10 + (str.charAt(i) - '0');
if (value > Integer.MAX_VALUE) {
throw new Exception("OUT OF INTEGER RANGE");
}
}
else {
throw new NumberFormatException("not an integer");
}
}
return (int) (isNeg == true ? value * -1 : value);
}

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

public int stringToInt(String s) throws Exception {

int val = 0;
int isNegative = 1;
String ss;
if (s.length() == 0) {
throw new Exception("Empty is not allowed.");
}

if (s.charAt(0) == '-') {
ss = s.substring(1);
isNegative = -1;
} else {
ss = s;
}

if (ss.length() == 0) {
throw new Exception("Invalid string.");
} else {
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i) - '0' < 0 || ss.charAt(i) - '0'> 9) {
throw new Exception("Invalid string");
} else {
val = val * 10 + (int)ss.charAt(i);
}
}
}
return val * isNegative;
}

- Shutian Li March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringToInt {

static Hashtable<Character, Integer> map = new Hashtable<Character, Integer>();
public static void main(String[] args) {

String s = "12367686";
buildMap();
System.out.println(convert(s));

}

public static void buildMap(){

map.put('0', 0);
map.put('1', 1);
map.put('2', 2);
map.put('3', 3);
map.put('4', 4);
map.put('5', 5);
map.put('6', 6);
map.put('7', 7);
map.put('8', 8);
map.put('9', 9);

}

public static int convert(String s){

int sum = 0;
for(int i = 0 ; i < s.length() ; i++){
sum *= 10;
sum = sum + map.get(s.charAt(i));
}

return sum;
}



}

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

public static int strToInt(String s) throws NullPointerException, NumberFormatException {
if (s == null) {
throw new NullPointerException();
}
s = s.trim();

if (s.length() == 0) {
throw new NumberFormatException("too short");
}

int sign = 1;
if (s.charAt(0) == '-') {
sign = -1;
s = s.substring(1);
} else if (s.charAt(0) == '+') {
sign = 1;
s = s.substring(1);
} else if (s.charAt(0) >= '0' && s.charAt(0) <= '9') {
sign = 1;
} else {
throw new NumberFormatException("illegal format at position 0");
}

if (s.length() == 0) {
throw new NumberFormatException("too short");
}

int len = s.length();
int lastRes = 0;
int currRes = 0;

for (int i = 0; i < len; i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') {
throw new NumberFormatException("illegal number at postion " + i);
}
currRes = currRes * 10 + s.charAt(i) - '0';
if (currRes < lastRes) {
if (sign == 1)
throw new NumberFormatException("overflow");
else
throw new NumberFormatException("underflow");
}
lastRes = currRes;
}
return currRes * sign;
}

- Yue March 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.mr.coooper.practice;

public class Practice7 {


private static int getMultipler(int limit){
int val = 1;
for(int j =0; j< limit; j++) {
val *= 10;
}
//System.out.println(val);
return val;
}

public static void main(String[] args) {
String str = "17s56";
boolean invalid = false;
int integerVal = 0;
int len = str.length()-1;
for( int i = 0; i<=len; i++) {
int tmp = 0;
char intVal = str.charAt(i);
switch (intVal) {
case '1':
tmp = getMultipler(len-i);
integerVal += (1 * tmp);
break;
case '2':
tmp = getMultipler(len-i);
integerVal += (2 * tmp);
break;
case '3':
tmp = getMultipler(len-i);
integerVal += (3 * tmp);
break;
case '4':
tmp = getMultipler(len-i);
integerVal += (4 * tmp);
break;
case '5':
tmp = getMultipler(len-i);
integerVal += (5 * tmp);
break;
case '6':
tmp = getMultipler(len-i);
integerVal += (6 * tmp);
break;
case '7':
tmp = getMultipler(len-i);
integerVal += (7 * tmp);
break;
case '8':
tmp = getMultipler(len-i);
integerVal += (8 * tmp);
break;
case '9':
tmp = getMultipler(len-i);
integerVal += (9 * tmp);
break;
case '0':
System.out.println("Don't take any action since it is ZERO");
break;
default:
System.err.println("Invalid string. => "+str);
invalid = true;
break;
}
if(invalid)
break;
}
if(!invalid)
System.out.println("String value => "+str +" ; Interger value => " + integerVal);

}
}

- Ashwin February 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Practice7 {
private static int getMultipler(int limit){
int val = 1;
for(int j =0; j< limit; j++) {
val *= 10;
}
return val;
}

public static void main(String[] args) {
String str = "17s56";
boolean invalid = false;
int integerVal = 0;
int len = str.length()-1;
for( int i = 0; i<=len; i++) {
int tmp = 0;
char intVal = str.charAt(i);
switch (intVal) {
case '1':
tmp = getMultipler(len-i);
integerVal += (1 * tmp);
break;
case '2':
tmp = getMultipler(len-i);
integerVal += (2 * tmp);
break;
case '3':
tmp = getMultipler(len-i);
integerVal += (3 * tmp);
break;
case '4':
tmp = getMultipler(len-i);
integerVal += (4 * tmp);
break;
case '5':
tmp = getMultipler(len-i);
integerVal += (5 * tmp);
break;
case '6':
tmp = getMultipler(len-i);
integerVal += (6 * tmp);
break;
case '7':
tmp = getMultipler(len-i);
integerVal += (7 * tmp);
break;
case '8':
tmp = getMultipler(len-i);
integerVal += (8 * tmp);
break;
case '9':
tmp = getMultipler(len-i);
integerVal += (9 * tmp);
break;
case '0':
System.out.println("Don't take any action since it is ZERO");
break;
default:
System.err.println("Invalid string. => "+str);
invalid = true;
break;
}
if(invalid)
break;
}
if(!invalid)
System.out.println("String value => "+str +" ; Interger value => " + integerVal);

}
}

- Ashwin February 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hola peeps,


In total awe…. So much respect and gratitude to you folks for pulling off such amazing blogs without missing any points on the
Amazon Interview Question for Software Engineer / Developers . Kudos!


Looking at the announcement of new service AWS Connect, I’d like to share few thoughts as an AWS existing customer and want to know what other people think.




I am so grateful for your blog. Really looking forward to read more.


Kind Regards,
Irene Hynes

- Irene Hynes June 12, 2018 | 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