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();
	}

- xdoer October 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Ignores the condition when 0 is the input.

- aes October 05, 2012 | Flag
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);
}
}

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

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);
}

- Manish October 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- tom.thkwok October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

how you make it?

- tom.thkwok October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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");
}

}

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

I am afraid there is still a extra zero.

- tom.thkwok October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, this is a good algorithm.

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

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

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

- JustCoding October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- code756 October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- JustCoding October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

- code756 October 07, 2012 | Flag
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();
}

- noobProg October 06, 2012 | Flag Reply
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;

- Sandy October 08, 2012 | Flag Reply
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;
}

- Rama Chandu October 10, 2012 | 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