Interview Question for Software Engineer in Tests


Country: England
Interview Type: Phone Interview




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

(n & (n-1) == 0) => n is power of 2

- Arun April 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct!

- xbirkettx April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yups!

- Harish April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Classic ol' trick. For those who don't know, the reason it works is because a power of two has the binary form 1000000... (with some number of 0s). The number minus 1 has the form 0111111...(with the corresponding number of 1s). Therefore ANDing the two would yield 0. This can only be true of powers of two because for any number that has any digits after the leading 1, both n and n-1 would have the leading one in the same place.

- eugene.yarovoi April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanx eugene.yarovoi for explaining the trick . Awesome trick.

- Abhi April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

'0' isn't power of two. More correct version.

boolean isPowerOfTwo(int value){
 	return value > 0 && (value & (value-1)) == 0;
 }

- m@}{ April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- Punit Jain April 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Worth pointing out that we need to add a check that n!= 0 too, as this condition evaluates to true for n=0. If this function was only for positive integers, then it's entirely correct as is.

- eugene.yarovoi November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

log2(number) has to be an integer
example:- log2(4) = 2

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

Correct but a couple hundred times less efficient than the other solution.

- eugene.yarovoi April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

awesome solution..

- Reddy April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct version which handle '0' and negative values properly.

boolean isPowerOfTwo(int value){ 		
 		if( value > 0 ){
 			return false;
 		} 		
 		if( value < 0 ){
 			value = -value;
 		} 		
 		return (value & (value-1)) == 0;
 	}

- m@}{ April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (!0+number == number-1){
return true;
else return false;
}

- Debdeep April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (!0+number == number-1){
return true;
else return false;
}
(has the problem of overflow though)

- Debdeep April 25, 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