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;
}

- Victor December 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its nice little trick

- Stuart December 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);
}

- 0xF4 December 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;

- kdr213 August 10, 2016 | Flag
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");
    }

- deepak.hebbar December 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Aman June 20, 2015 | Flag Reply
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;
}

- hilleybilley January 23, 2016 | Flag Reply
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");
}

- justin March 24, 2018 | Flag Reply
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");
}

- justin March 24, 2018 | Flag Reply
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.

- swapnilkant11 June 13, 2019 | 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