## Amazon Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

``````public static void decimalToBinary2(int decimal, StringBuffer buf){
if( decimal> 0){
int nextDecimal = decimal/2;
decimalToBinary2(nextDecimal, buf);
buf.append(decimal%2);
}
}``````

or an iterative version:

``````public static String decimalToBinary(int decimal){
Stack<Integer> bits = new Stack<Integer>();
while(decimal > 0){
bits.push(decimal%2);
decimal = decimal/2;
}

StringBuffer buf = new StringBuffer();
while(!bits.isEmpty()){
buf.append(bits.pop());
}
return buf.toString();
}``````

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

Ignores the condition when 0 is the input.

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

void convertDecimalToBinary(int num)
{
int temp;
if(num)
{
temp = num%2;
convertDecimalToBinary(num>>=1);
printf("%d ",temp);
}
}

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

void convertDecimalToBinary(int num)
{
int temp = num; // to take care of scenario when num is zero
if(num) {
temp = num%2;
convertDecimalToBinary(num>>1);
}
printf("%d ",temp);
}

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

but then it will output one more zero in the end?

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

@Tom:We can write else printf("Temp");..Then extra zero wont come..

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

how you make it?

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

public static void convert(int num)
{
int temp = num%2;
if(num>0)
{
convert(num>>1);
System.out.print(temp);
}
else
{
System.out.print("0");
}

}

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

I am afraid there is still a extra zero.

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

Hi, this is a good algorithm.

What if there is a decimal part in the given number eg - 114.5678?

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

binary numbers are >= 1 only... so decimal point and beyond are to be ignored... cos anything after the decimal (that is .anything) is < 0...

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

@justCoding : no we do have decimal numbers in Binary system.

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

@code756: so can you explain what the binary form of 10.2 will be like?

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

@just coding : it is 1010.00110011.....

U can find the explanation by googling the phrase 'binary conversion after decimal'. Click 3rd link.

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

``````#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
int num;
char ch[100];
char temp;
int i=0,j,len=0;
printf("Enter the Number\n");
scanf("%d",&num);
while(num!=1)
{
if(num%2==0) { ch[i++]='0';}
else{ch[i++]='1';}
num=num/2;
}
ch[i++]='1';ch[i]='\0';
len=strlen(ch);
i=0;j=len;
// Reverse String
for (i=0,j=len-1;i<j;i++,j--)
{
temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
}
printf("The Binary Rep is= "); puts(ch);printf("\n");
getch();
}``````

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

Simple Solution. Divide repeatedly till num>1. Append mods to string buffer.

Final String = Integer.toString(num) + modBuffer.reverse().toString()

If 0 return 0;

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

My soln:
--------------------
getDecimal(value Binary) {
nowE = 0, final_val=0;
while(Binary > 0){
finVal += (Binary%10) * (2 ^ nowE);
Binary = Binary/10;
}
return finVal;
}

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.

### 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.