Amazon Interview Question for Software Engineer / Developers






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

public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 2 ; i < 10000; i++) {
int j = i&(i-1);
if(j == 0) {
System.out.println("2s Power:: "+i);
}
}
}

- yashddn March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {

BufferedReader reader=new BufferedReader(
new InputStreamReader(System.in) );



while(true)
{
System.out.print("input integer:");
String line= reader.readLine();

if(line.isEmpty())
{
break;
}

int digit=Integer.parseInt(line);
System.out.println("processing : "+ digit );

boolean isP=checkPowerOf2(digit);

if(isP)
{
System.out.println(digit+" is power of 2");
}else
{
System.out.println(digit+" is NOT power of 2");

}

}


reader.close();


}

private static boolean checkPowerOf2(int digit) {
long p2=2;

while(true)
{
if(p2>digit)
{
return true;
}


if(digit%p2!=0)
{
return false;
}

p2*=2;
}




}

}

- HertzLee February 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u can easily try in c or c++!!!!

- deepak.mit4612 February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

F**cking solution !!

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

count number of bits == 1.
if not==1 then not power of 2

- blueskin February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but the answer given at the top is of o(1)...

- sarathprasath February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sarathprasath: it looks like O(1).
counting bits is faster than applying '&' between two bits. If you look into the details "num & (num-1) == 1" has more complexity than counting number of bits

- Troll February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

which operation are you going to use to count bits?

AND op can be calculated in one clock cycle by processors. how is counting bits faster than that?

- Anonymous February 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we check if(num%2 == 0) then print power of 2. Else not.

- srikar February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

6 % 2 = 0, so 6 is power of 2?

- Anonymous February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@srikar : hahaha..

- Anonymous February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brains :)

- Anonymous May 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The person who posted the question has given answer in O(1) time complexity. I don't know, why HertzLee answered it differently.

- Karthik February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Karthik; just ignore HertzLee.
I don't understand what you mean by O(1) in this case. can you explain how it is O(1)

- Troll February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

check for 0 must be there. ) is not power of 2.
if((!num) && (!(num & num-1))

- Tulley February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Corrected
if((num) && (!(num & num-1))

- Tulley February 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry but could you please explain how it works? Could not understand the logic behind it. Any inputs would be of great help. Thanks

- johnny February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

8 = 1000
7 = 0111

8&7 = 0000

- blakdogg February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For all the folks who think the answer posted with the question is right: IT IS PLAIN WRONG.
As Tulley pointed out it should be "if((num) && (!(num & num-1))"
But looking at blueskin's solution: counting number of bits and if equals to 1 or break counting when greater than 1 is the best solution because counting bits is faster than applying bit operations between two integers

- GekkoGordan February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't this work?
if(number<<1 == 0)
return true;
else
return false;

- anirudh February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, it does not. Trivial mistake

- anirudh February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't understand how counting bits is faster than a single AND operation!

- Anonymous March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If is allowed to use library functions : Find out the log(base 2 ) of the input, check whether integer or not.

- sasa February 22, 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