Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

{{
int x=0;
boolean negative = false;
String out="";
System.out.println("Please enter the input");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
if(x<0){
negative = true;
}
x = Math.abs(x);
while(x>0){
out = x%10+out;
x =x/10;
}
if(negative)
out = "-"+out;
System.out.println(out);
}}

- Denis March 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

private static String convertIntegerToString(Integer num) {
		List<String> stack = new LinkedList<>();
		boolean negetive = num < 0 ? true : false;
		if (num < 0) {
			num = (num * -1);
		}
		while (num > 0) {
			stack.add(num % 10 + "");
			num = num / 10;
		}
		if(negetive)
			stack.add("-");
		StringBuilder sb = new StringBuilder();
		for (int i = stack.size() - 1; i >= 0; i--) {
			sb.append(stack.get(i));
		}
		return sb.toString();
	}

- Arpan Das May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

private static String convertIntegerToString(Integer num) {
		List<String> stack = new LinkedList<>();
		boolean negetive = num < 0 ? true : false;
		if (num < 0) {
			num = (num * -1);
		}
		while (num > 0) {
			stack.add(num % 10 + "");
			num = num / 10;
		}
		if(negetive)
			stack.add("-");
		StringBuilder sb = new StringBuilder();
		for (int i = stack.size() - 1; i >= 0; i--) {
			sb.append(stack.get(i));
		}
		return sb.toString();

}

- Arpan Das May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++, implementation, O(n)

string intToString(int n) {
	string str;
	int m, k;

	if (n < 0) {
		str = "-";
		m = -n;
	} else {
		str = "";
		m = n;
	}

	k = 1;
	while ((m / k) >= 10) {
		k *= 10;
	}

	while (k > 0) {
		str += (m / k) + '0';
		m %= k;
		k /= 10;
	}

	return str;
}

- kyduke March 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is an easy way to solve this using recursion

#include <stdio.h>

void string_value(int);

int main(void)
{
    int input = 0;
    printf("Provide Input (signed int)\n");
    scanf("%d",&input);
    string_value(input);
    return 0;
}

void string_value(int num)
{
    int rem = 0;
    char out = '0';
    
    if(num < 0)
    {
        num = (int)(-1)*num;
        out = '-';
        putchar(out);
    }
    
    rem = num%10;
    num = num/10;
    
    if(num !=0)
        string_value(num);

    out = rem + '0';

    putchar(out);
}

- godzila March 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void string_value(int);

int main(void)
{
    int input = 0;
    printf("Provide Input (signed int)\n");
    scanf("%d",&input);
    string_value(input);
    return 0;
}

void string_value(int num)
{
    int rem = 0;
    char out = '0';
    
    if(num < 0)
    {
        num = (int)(-1)*num;
        out = '-';
        putchar(out);
    }
    
    rem = num%10;
    num = num/10;
    
    if(num !=0)
        string_value(num);

    out = rem + '0';

    putchar(out);
}

- godzilathakur March 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It doesn't talk about how exactly it's happening

- SIVA R March 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int is_numeric(char x)
{
	return (x >= '0') && (x <= '9');
}

int char_to_num(char x)
{
	return (x - '0');
}

int convert_to_number(char* num_str, int *value)
{
	int len = strlen(num_str);
	int i, result = 0;
	for (i = 0; i < len; i++) {
		if (is_numeric(num_str[i])) {
			result = (result * 10) + char_to_num(num_str[i]);
		}
	}

	if (num_str[0] == '-') {
		result *= -1;
	}

	*value = result;
	return 0;
}

int main(int argc, char* argv[])
{
	int ret = 0, value;
	ret = convert_to_number(argv[1], &value);
	if (ret == EXIT_FAILURE) {
		printf("Invalid number: %s\n", argv[1]);
	}
	else {
		printf("Value is: %d\n", value);
	}
	return EXIT_SUCCESS;
}

- simple coder March 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First consider the corner case -2147483638, it will overflow of int if convert it to positive 2147483638.
Then use a stack to reverse output each bit of the number.

string ItoA(int a){
	if (a == INT_MIN)
		return "-2147483648";
	if (!a)
		return "0";
	string result = "";
	if (a < 0){
		a = - a;
		result += '-';
	}
	stack<int> num;
	while (a){
		num.push(a % 10);
		a /= 10;
	}
	while (!num.empty()){
		result += '0' + num.top();
	}
	return result;
}

- kuiwy7 March 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String valueOf(Integer input) {
	if (input == null) return null;
	if (input == 0) return input+"";
	StringBuilder sb = new StringBuilder();
	boolean negative = input < 0;
	int remainingNum = input * (negative ? -1 : 1);
	while (remainingNum % 10 != 0) {
		sb.append((remainingNum % 10) + "");
		remainingNum = currentNum / 10;
	}
	if (negative) {
		sb.append("-");
	}
	return sb.toString().reverse();
}

This would be O(n) where n is the number of digits in the number.

- dl March 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String intToString(int n) {
String str="";
if (n < 0) {
str = "-";
n *= -1;
}
str += n;

return str;
}

- Mohammed Arif March 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String valueOf(int num) {
		String signed = "";
		if (num < 0) {
			signed = "-";
		}
		return signed + numToString(Math.abs(num));
	}

	String numToString(int num) {
		if (num == 0) {
			return "0";
		}
		String result = "";
		while (num > 0) {
			result = ((char) num % 10) + result;
			num = num / 10;
		}
		return result;
	}

- Johnny March 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void convertToString(int aNumber){
    
    int lLengthOfString = 0;
    if(aNumber < 0)lLengthOfString++;
    
    int base = 1;
    int lAbsolteNumber = aNumber >0 ?aNumber:-1*aNumber;
    
    while(lAbsolteNumber/base > 0){
        base *=10;
        lLengthOfString++;
    }
    
    if(aNumber < 0){
        lLengthOfString++;
    }
    
    // sign + \0
    char* result = new char[lLengthOfString+1];
    int i=0;
    
    if(aNumber < 0){
        result[0]='-';
        i=1;
    }
    
    base /= 10;
    
    while(base > 0 && lAbsolteNumber/base > 0){
        int lResult = (lAbsolteNumber/base);
        result[i++]= (char) '0' + lResult;
        lAbsolteNumber-=(lResult*base);
        base/=10;
        if(base > 0 && lAbsolteNumber/base == 0 ){
            base/=10;
            result[i++]='0';
        }
    }
    result[i++] = lAbsolteNumber%10;
    result[i] = '\0';
    
    printf("%s\n",result);

}

- ravinder.chouhan March 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 1352;
String str = i + "";

- Asif Garhi March 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a little bit tricky since most of the people has decided to use append in programming language which lead us into a problem numbers are not the same in characters as they are in number (this depends on charactermap you are using)
I java this can be done as:

String convertToString(int n){
		int temp = n;
		boolean isNegative = n<0? true: false;
		StringBuffer buffer = new StringBuffer();
		int divider=10;
		if(isNegative ){
			temp *= -1;
		}
		while(temp >0){
			int number = temp % divider;
			temp = temp/divider;
			buffer.insert(0,(char)(number+'0')); 
		}
		if(isNegative ){
			buffer.insert(0,'-'); 
		}
		return buffer.toString();
		
	}

- Uriel Rodriguez March 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the ideas are fine and close to the point but integers are not directly mapped to characters, in all approaches you are abusing on the idea that integers can be appended to string this is an implicit conversion.
That means that is the same as returning ""+number;

static String convertToString(int n){
		int temp = n;
		boolean isNegative = n<0? true: false;
		StringBuffer buffer = new StringBuffer();
		int divider=10;
		if(isNegative ){
			temp *= -1;
		}
		while(temp >0){
			int number = temp % divider;
			temp = temp/divider;
			buffer.insert(0,(char)(number+'0')); 
		}
		if(isNegative ){
			buffer.insert(0,'-'); 
		}
		return buffer.toString();
		
	}

- Uriel Rodriguez March 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the approach is the correct, but you are relying too much on auto conversion since are appending integer to string this does not makes any difference between doing the division to return something like ""+number;

you need to consider that characters that represent numbers are not directly mapped to real numbers

static String convertToString(int n){
		int temp = n;
		boolean isNegative = n<0? true: false;
		StringBuffer buffer = new StringBuffer();
		int divider=10;
		if(isNegative ){
			temp *= -1;
		}
		while(temp >0){
			int number = temp % divider;
			temp = temp/divider;
			buffer.insert(0,(char)(number+'0')); 
		}
		if(isNegative ){
			buffer.insert(0,'-'); 
		}
		return buffer.toString();
		
	}

- urodba March 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String ConvertIntToString(int n)
{
if(n==0)
return 0;

StringBuilder newstring=new StringBuilder();
while(n>0)
{
int a=n%10;
n=n/10;
newstring.append(a);
}
string b= newstring.substring(0);
newstring=new StringBuilder();
for(int =b.length()-1;i>=0;i--)
{
newstring=append(b.charat(i));

}
return newstring.substring(0);
}

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

This is a code in C++ using recursion.

stringcon.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
#include<string>

using namespace std;

string recurse(int n) {
string s = "";
if (n != 0) {
int x = n % 10;
char c = '0' + x;
n = n / 10;
s = recurse(n);
if (!s.empty())
s = s + c;
else {
string s;
s.push_back(c);
return s;
}

}
return s;
}
void func(int n) {
string out = "";
if (n<0) {
out += "-";
out += recurse(-n);

}
else
{
out += recurse(n);
}

out = out + '\0';
cout<<out;
}
int main()
{
int n = -12356;
cout << "String is ";
func(n);
getchar();
return 0;
}

- Naman Gupta March 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{public class IntToString {
public static void main(String[] args) {
int i=-123;
System.out.println(String.valueOf(i));
String str=""+i;
System.out.println(str);
}
}
}

- Rahul Lakhmara April 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't it too simple ? why so much complication ?

public static String valueOf(int num){
        String str = ""+num;
        return str;

    }

- Hero April 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class IntToString {
	char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999 };

	public String toString(int num) {

		int size = 0;
		boolean symbol = false;
		if (num < 0) {
			symbol = true;
			size = 1;
			num = -num;
		}
		int maxPosition = sizeTable.length - 1;
		if (num > sizeTable[maxPosition]) {
			throw new RuntimeException("Number should be < "
					+ sizeTable[maxPosition]);
		}

		for (int i = 0; i <= maxPosition; i++) {
			if (num <= sizeTable[i]) {
				size = size + i + 1;
				break;
			}
		}

		char[] nums = new char[size];
		if (symbol) {
			nums[0] = '-';
		}
		int i = size - 1;
		while (num != 0) {
			int d = num - (num / 10) * 10;
			num = num / 10;
			nums[i--] = number[d];
		}

		return new String(nums);
	}

	public static void main(String[] args) {
		IntToString to = new IntToString();
		System.out.println(to.toString(-9999999));
		
	}

}

- Sami April 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def num2str(num):
    s = ""
    tmp_num = abs(num)
    while tmp_num > 0:
        digit = tmp_num % 10
        s = str(digit) + s
        tmp_num = int(tmp_num / 10)
    s = s if num > 0 else "-" + s
    return s

- Newbie April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String convertIntegerToString(Integer num) {
		List<String> stack = new LinkedList<>();
		boolean negetive = num < 0 ? true : false;
		if (num < 0) {
			num = (num * -1);
		}
		while (num > 0) {
			stack.add(num % 10 + "");
			num = num / 10;
		}
		if(negetive)
			stack.add("-");
		StringBuilder sb = new StringBuilder();
		for (int i = stack.size() - 1; i >= 0; i--) {
			sb.append(stack.get(i));
		}
		return sb.toString();

}

- Arpan Das May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String convertIntegerToString(Integer num) {
		List<String> stack = new LinkedList<>();
		boolean negetive = num < 0 ? true : false;
		if (num < 0) {
			num = (num * -1);
		}
		while (num > 0) {
			stack.add(num % 10 + "");
			num = num / 10;
		}
		if(negetive)
			stack.add("-");
		StringBuilder sb = new StringBuilder();
		for (int i = stack.size() - 1; i >= 0; i--) {
			sb.append(stack.get(i));
		}
		return sb.toString();
	}

- arpan.kgp May 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class ConvertIntToStriing {
	public static void main(String[] args) {
		int in1=303534;
		StringBuilder res=new StringBuilder();
		if(in1 < 0){
			res.append("-");
			in1=in1*-1;
		}
		while(in1 > 0){
			res.append(in1%10);
			in1=in1/10;
		}
		System.out.println(res);
	}
}

- yugandharr147 March 14, 2016 | 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