Amazon Interview Question
Software Engineer in TestsCountry: United States
Interview Type: Phone Interview
stack<int> getDigits(int b)
{
stack<int> digits;
int x, digit;
int a = b;
while (a > 10)
{
x = (a / 10);
digit = a - (x * 10);
digits.push(digit);
a = x;
}
digits.push(a);
return digits;
}
//int StringToNum(string s)
//{
//
//}
string NumToString(int a)
{
string s;
stack<int> digits = getDigits(a);
int size = digits.size();
for (int i = 0; i < size; i++)
{
int num = digits.top();
s.push_back(num +48);
digits.pop();
}
return s;
}
#include <iostream>
using namespace std;
char int_to_char[10] = { '0','1', '2', '3', '4', '5', '6', '7', '8', '9' };
char* toString(int i){
if (i == 0){
char* c = new char[1];
*c = '0';
return c;
}
char* r;
int absolute = i;
if (i < 0){
absolute = -1 * absolute;
}
int len = 0;
while(absolute > 0){
len++;
absolute = absolute/10;
}
absolute = i;
if (i < 0){
absolute = -1 * absolute;
len++;
}
r = new char[len];
while(absolute > 0){
r[--len] = int_to_char[absolute % 10];
absolute = absolute/10;
}
if (i < 0){
r[--len] = '-';
}
return r;
}
int main()
{
cout << toString(-100);
// handle delete of allocations
return 0;
}
#include <iostream>
using namespace std;
char int_to_char[10] = { '0','1', '2', '3', '4', '5', '6', '7', '8', '9' };
char* toString(int i){
if (i == 0){
char* c = new char[1];
*c = '0';
return c;
}
char* r;
int absolute = i;
if (i < 0){
absolute = -1 * absolute;
}
int len = 0;
while(absolute > 0){
len++;
absolute = absolute/10;
}
absolute = i;
if (i < 0){
absolute = -1 * absolute;
len++;
}
r = new char[len];
while(absolute > 0){
r[--len] = int_to_char[absolute % 10];
absolute = absolute/10;
}
if (i < 0){
r[--len] = '-';
}
return r;
}
int main()
{
cout << toString(0);
// handle delete of allocations
return 0;
}
import java.util.Stack;
public class TestProgramming {
public static void main(String ar[]) {
int i=2345678;
Stack<Integer> stack = new Stack<Integer>();
while(i>0) {
int curr=i%10;
i=i/10;
stack.push(curr);
}
StringBuilder sbr=new StringBuilder();
while(!stack.isEmpty())
sbr.append(stack.pop());
String str=sbr.substring(0);
System.out.println(str);
}
}
char* numToString(int num)
{
static char output[32]={0,};
static char reverse[32]={0,};
int n = num, idx = 0;
while(n)
{
output[idx] = 48 + (n%10);
idx++;
n = n/10;
}
output[idx]='\0';
int len = idx;
//reverse the string
idx = 0;
printf("\n reversed string = ");
while(idx != len)
{
reverse[idx] = output[len-idx-1];
printf("%c", reverse[idx]);
idx++;
}
reverse[idx]='\0';
return reverse;
}
public String convert(Integer value){
- Chaitanya Srikaolapu November 18, 2019return value+"";
}