Amazon Interview Question






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

int binary(int n)
{
if(n==0)
return 0;
else
{
binary(n/2);
printf("%d",n%2);
}

return 0;
}

- Anonymous April 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it is obviously correct

- Chintalapudi Rajasekhar April 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

-1 this is wrong , you didn't consider negative value

- Charles January 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can do this with positive number , but you really can't do it without negative number , how do you fill -1 ?

- Charles January 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I vote +1 for your code again since I found a little tweak in your code will handle the negative number

change
int binary(int n)
to
int binary(unsigned int n)

- Charles January 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I may not understand the question properly. I think following java code can solve this problem.
public static String toBinaryString(int k) {
if (k == 1) {
return String.valueOf(k);
} else {
return toBinaryString(k / 2) + k % 2;
}
}
Please correct and help me solve it out.

- brijpal.ism April 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursion will help here

void PrintBinary(int n)
{
if(n!=0)
{
PrintBinary(n/2);
printf("%c", (n%2)+'0');
}

}

- Virendra April 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain the logic ?
why do you add '0' ?

- Anonymous April 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is for adding 48 to make it character.

- ankushbindlish May 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Amazon2{
	public static void main(String args[])
	{
		int n =5;
		printBinary(n);
	}
	private static void printBinary(int n)
	{
		if(n==0) return;
		printBinary(n/2);
		System.out.print(n%2);
	}
}

- Anonymous April 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wont this print out the reverse of the binary number?
I mean if 12 is 1100 then wouldn't the program print 0011???

- Anonymous April 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ 4 lines of code should do it.. int FindBinary(int N) { if(N/2>0) FindBinary(N/2); printf("%d",N%2); } - Anonymous April 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this solution

public class Binary {
	public static void main(String[] args) {
                //Read in the variable somehow
		int var = 34;

		printVar(var);
	}

	private static void printVar(int var) {
		if (var == 1) {
			System.out.print(var);
		} else {
			printVar(var >> 1);
			System.out.print(var & 1);
		}
	}
}

- Chintan April 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All are writing same code, same logic, and adding one their own trademark defect. For instance - function returning integer doesn't return anything.

I believe we are good to close this thread and all understand the solution.

Thanks
Ankush

- ankushbindlish May 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int binary(int n)
{
	if(n==0||n==1) cout << n;
	else
	{
		binary(n/2);
		cout << n%2;
	}
}

- Kartik May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How bout this code.....

#include<stdio.h>

void Print_Binary(int);

void Print_Binary(int n)
{
if(n>=2)
Print_Binary(n/2);
printf("%d",n%2);
}

main()
{
int n;
printf("Enter the value of n\n");
scanf("%d",&n);
Print_Binary(n);
getch();
}

- kunal.kumar8808 July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i agree with ankushbindlish..all codes are same and they all have same bug..that is for n=10,the output is in reverse order instead of the correct output..give a solution which will give correct output..

- Anant October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

say the integer has 32 bits.

unsigned int mask = (1 << 31);
while(mask > 0) {
print mask & num ? '1' : '0';
mask >> 1;
}

- AV November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

read mask = mask >> 1;

- AV November 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@anant- Are you nuts? the o/p for the recursion procedure is in the correct order...

- vg November 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Everyone is providing the correct solution in different languages. The key point here is Recursion calling of the same function by dividing the number by 2 and passing the same. I think we can close the thread

- Chintalapudi Rajasekhar May 11, 2011 | 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