## Monotype Interview Question for Senior Software Development Engineers

Country: India
Interview Type: In-Person

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

A power of 2, when represented as binary number, is one bit 1 with all the remaining bits 0.

This code below flips the rightmost bit 1 of an integer. If the resulting number becomes zero, you know it had only one bit 1 before, thus a power of 2.

``````int power2(int a)
{
a = a & (a-1);

return a == 0;
}``````

Comment hidden because of low score. Click to expand.
0

Its nice little trick

Comment hidden because of low score. Click to expand.
0

The code above is incorrect, since it doesn't handle an important edge case [of 0 and negative numbers].
Handling of such edge cases is important for these kind of interview questions. Here's correct version:

``````bool power2(int a)
{
return (a > 0) && ((a & (a-1)) == 0);
}``````

Comment hidden because of low score. Click to expand.
0

A power of 2 is never a negative number.
2^1 = 2
2^0 = 1
2^(-1) = 1/2
...
2^(-inf) = 0

Comment hidden because of low score. Click to expand.
0

You're thinking of a multiple of 2. A power of two, in binary, will be represented by 00000010, 00000100, 00001000, 00010000, etc. Thus you need to count the # of bits. If it's more than one (you can stop when you get to 2), it's not a power of 2. Or if the 1's place is 1. So it would look like this:

``````if(a&1) return false;
for(i = 0; i < 32; i++) {
if(a = a>>1  & 1)count ++;
if (count>1) return false;
}
if count = 1 return true;
else return false;``````

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

``````int powerTobeFound = 2147483648;
if ((powerTobeFound & (~powerTobeFound+1)) == powerTobeFound) {
NSLog(@"Power of TWO");
}``````

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

``return !a && a & (a - 1)``

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

``````#include <iostream>

using namespace std;

int main()
{

int powerOfTwo;

char exit = 'n';

while(  exit=='n')
{
cout << "Enter number :";
cin>> powerOfTwo;

if(powerOfTwo==0)
{
cout << "Number is 0 only.";
}
if(!(powerOfTwo &(powerOfTwo-1)))
{
cout<<"Number is Power of 2..."<<endl;
}
else{
cout<<"Number is not power of 2..."<<endl;
}

cout << "Do you want to exit ? Y/N" << endl;
cin >>exit;
}

return 0;
}``````

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

#include<stdio.h>

main()
{
int a;
int o;
printf("enter # \n");
scanf("%d", &a);
o=-a;

if((a&o)==a) // get two's complement of the number and & with the given number gives
printf(" YES \n"); //the same number if its power of 2
else
printf(" NO \n");
}

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

``````#include<stdio.h>

main()
{
int a;
int o;
printf("enter # \n");
scanf("%d", &a);
o=-a;

if((a&o)==a)
printf(" YES \n");
else
printf(" NO \n");
}``````

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

The most effective algorithm to check for the number is a power of 2 or not we will follow the below implementation:

Implementation:

return x && (!(x&(x - 1)));

which will return either false for the number is not a power of 2 or will return true if it is.

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.

### 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.