Amazon Interview Question for Software Engineer / Developers






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

f = !(v & (v - 1)) && v;

- cougar_cs November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int power_of_two(int x) {
if (x % 2)
return null;

return power_of_two(x/2);
}

- S November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int powerOfTwo(int n) {
int count = 0;
while(n) {
n = n&(n-1);
count++;
}
if (count <= 1)
return 1;
else
return 0;
}

- Anonymous November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int powerOfTwo(int n) {
//If we have have more than 1 bit set return false
if ( n&(n-1) )
return 0;
return 1;
}

- Stephen November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is wrong.

what if n=0

- Anonymous November 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int powerOfTwo(int n) {
//If we have have more than 1 bit set return false
return !(n & (n-1) );
}

- Anonymous November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

could somebody explain why only check n&(n-1)is OK?
Thanks.

- Anonymous November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

coz when you do (n & (n-1)), the resulting binary number will have number of 1 bits exactly one less than original number. Try it your self by playing with some numbers. So, If (n & (n-1)) is zero, it means that n has only one 1-bit. Any number that has exactly one 1-bit, is power of two.

- Neo November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree. eg.
Suppose n = 8
bit format: 1000
n-1 = 7
bit format: 0111

x & (n-1) = 0

suppose n = 9
bit format: 1001
n-1 = 8
bit format: 1000

x & (n-1) = 1000 != 0

- Saga Yao March 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Some more bit manipulation

int n;
take input in n;

n <<= 31;
n >>= 31;

if(n&1)
print odd;
else print even;

- Anonymous January 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

or you can simply do:

take input in n
if(n&1)print odd
else print even

- Anonymous January 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

n&(n-1) logic only works if n > 1.

- Anonymous February 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about checking the last bit?
(!(n & 1)) ==> Even
else Odd.

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

A number would be a power of two if there is exactly one bit that is equal to 1 in its bit pattern (even 1 is a power of 2. 2 raised to the power zero is 1). Now we could use the method that we used to count the number of bits that are 1 in a given number and check if it is equal to 1. However that approach needs many steps and hence won’t suffice here.

In a number which is an exact power of 2 only 1 bit is set and all others are zero. Let the position of this 1 bit be MSB. Mathematics rules for binary numbers tells us that if we subtract 1 from this number then the number that we would get would have all its bit starting from the bit position MSB+1 set to 1. For example if the given number num is 8(00001000) then num-1 would be 7 (00000111). Now we notice that these two bit patterns dont have a 1 in the same bit position. Further observation suggests that if we bitwise and (&) both these numbers we would get zero.

bool isPowerOf2(int num)
{
return ((num>0) && (num & (num-1))==0);
}

- Cookie May 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For a number to be a power of 2, only one of the bits has to be set and others zero. In the below code, you test the last bit if zero, you rotate the bits till you find 1.

bool testpowerof2(int i){
	if (i<=0)
		return false;
	while(i !=0){
		if (i & 1)
			break;
		i = i>>1;
	}
	if (i == 1)
		return true;
	return false;
}

- Neo September 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one is faster

bool testpowerof2Fast(int i){
	if ((i>0) && ((i & (i-1)) == 0))
		return true;
	return false;
}

- Neo September 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice buddy :)

- kirankumar.v009 September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
if(!(num & (num - 1)) && num) { // Power of 2! }
}

OR

{
if(((~i+1)&i)==i) { //Power of 2! }
}

- Anonymous November 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Method 1:

((x+(x-1))==((x|(x-1))))?printf("\nIt is"):printf("\nIt is Not");

Method 2:

((x&(x-1))==0?printf("\nIt is powers of 2"):printf("\nIt is not a power of 2"));

Method 3:

((x^(x-1))==((x*2)-1)?printf("\t\nIt is powers of 2"):printf("\nIt is not a power of 2"));

- get2jawa July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPowerOf2(int a){
String binaryNumber = Integer.toBinaryString(a);
return (binaryNumber.length()-binaryNumber.replace("1","").length() ==1);
}

- Pavanraj April 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPowerOf2(int a){
		String binaryNumber = Integer.toBinaryString(a);
		return (binaryNumber.length()-binaryNumber.replace("1","").length() ==1);
	}

- Pavanraj April 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the easiest algorithm for the above problem statement will take the & of the bitwise of the given number and (n - 1) which will result in 0 when the number n is the power of 2 else will result in 1 and then their ! will result in 1 and 0 respectively and && with the number will result in true value if no is power of two else will re turn false:

Implementation:

#include<bits/stdc++.h>
using namespace std;
bool findpoweroftwo(int x){
return x && (!(x & (x - 1)));
}
int main(){
if(findpoweroftwo(5))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}

- swapnilkant11 June 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (number > 0 && !(number&(number-1)))
                printf("power of 2\n");
        else
                printf("not power of 2\n");

- sukanya January 03, 2021 | 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