Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Written Test




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

C++ version.

#include <iostream>

std::string my_itoa(int n)
{
	bool neg = false;
	if (n < 0) {
		neg = true;
		n *= -1;
	}
	
	// special case: 0
	if (n == 0)
		return "0";
		
	std::string s;
	while (n > 0) {
		s.insert(0, 1, static_cast<char>('0' + n % 10));
		n /= 10;
	}
	
	if (neg)
		s.insert(0, "-");
		
	return s;
}

int main() {
	std::cout << my_itoa(0) << std::endl;
	std::cout << my_itoa(10) << std::endl;
	std::cout << my_itoa(25) << std::endl;
	std::cout << my_itoa(256) << std::endl;
	std::cout << my_itoa(-10) << std::endl;
	std::cout << my_itoa(-25) << std::endl;
	std::cout << my_itoa(-256) << std::endl;
	return 0;
}

- Diego Giagio November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You used static_cast(). Is that allowed?

- nitinsh99 February 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

loop {
rem = value % 10
value /= 10
mystr[i++] = rem - '0' //sizeof(mystr) == sizeof(int) + 1 and mystr[i] = '\0' for 0 < i < sizeof(mystr) 
} until value > 0 

rev(mystr) // reverse by swapping ith and (n-1-i)th for 0 < i < sizeof(mystr)/2

- Murali Mohan November 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Null terminated string? Should the function's return type be char* ? If so, the caller will need to worry about the memory problem then, which I don't think is a very good way. If the function returns a string as an object, then the null terminated request will be redundant. Here is my C++ solution:

string intToString(int n)
{
    if(n == 0) return "0";

    string s;
    if(n < 0){
        s.push_back('-');
        n = -n;
    }
    for(; n; n /= 10) s.push_back(n%10 + '0');

    char c;
    for(int i = s[0] == '-', j = s.size()-1; i < j; ++i, --j){
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
    return s;
}

- uuuouou November 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class numToStr {
public static void numToStrVal(int a,String s){ 
	if(a>0){
		s=a%10+s;
		numToStrVal(a/10,s);
	}
	else
	System.out.println(s);;
	
}
public static void main(String[] args) {
	numToStrVal(324,"\0");
}
}

- sudharshan December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its if(a>0);

- sudharshan December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

string itoa(int num){
	
	if (num == 0) return "0";
	string ret_string;
	
	if (num < 0){		
		ret_string.push_back('-'); 
		num = -num;
	}
	
	while (num !=0){
		ret_string.push_back(num%10 + '0');
		num = num/10;
	}

	ret_string.push_back('\0');

	//swap
	for(int i = ret_string[0] == '-', j = ret_string.size()-2; i <j ; i++, j--){
		ret_string[i] = ret_string[i] ^ ret_string[j]; 
        ret_string[j] = ret_string[i] ^ ret_string[j]; 
        ret_string[i] = ret_string[i] ^ ret_string[j]; 
	}

	return ret_string;
}

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

C++ version:

#include <iostream>
#include <string>

using namespace std;

string itoa(int i)
{
    return to_string(i);
}

int main() {
	std::cout << itoa(0) << std::endl;
	std::cout << itoa(10) << std::endl;
	std::cout << itoa(25) << std::endl;
	std::cout << itoa(256) << std::endl;
	std::cout << itoa(-10) << std::endl;
	std::cout << itoa(-25) << std::endl;
	std::cout << itoa(-256) << std::endl;
}

Note that itoa is not part of the standard, so I have no qualms about obliterating it in my namespace.

- Jackson Hardmaster November 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or try this:

string itoa(int i)
{
    string sign, magnitude;
    if(i < 0)
    {
        i = -i;
        sign = "-";
    }

    do
    {
        magnitude = static_cast<char>('0' + i%10) + magnitude;
        i /= 10;
    }while(i);

    return sign + magnitude;
}

- Jackson Hardmaster November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You must be kidding that you used std::to_string(). Do you think an interviewer will accept this? He wants you to implement that. That's the point of the question.

- Diego Giagio November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Diego, you can see my other solution just above your complaint.

- Jackson Hardmaster November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I saw that. Almost exactly like mine.

- Diego Giagio November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Diego, except it's better.

- jackyplus November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL @ jackson hardmaster

- Anonymous November 24, 2013 | Flag


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