Qualcomm Interview Question for Software Engineer in Tests






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

for unsigned integer nummbers:
bool isPow2(int n)
{
return !(n & (n-1)) && n;
}

- strezh October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

if number of bits set is 1 then it is a power of two

- chennavarri October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

(num & (num-1))?notpower:power;

- notytony September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for unsigned integer numbers:
bool isPow2(int n)
{
return !(n & (n-1)) && n;
}

- strezh October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x^x-1 if all bits are 1 then power of 2 else not.

- Praveen October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

x & x-1. Check if output is zero.
This is easy to check than checking all bits are one in case of x^x-1

- Akhil June 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

return num&(num-1)==0

- Xtra October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain it?

- Anonymous November 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

let us take num=8 (2 pow 3) and n-1 means 7
n =1000
n-1=0111 perform & operation
----------------
0000
------------- so 8 is power of 2

- vamsha July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

return ((pow(2,log2(n))-pow(2,floor(log2(n))))==0)

- chennavarri October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if n&(n-1)== 0, then number is power of 2.

- Anonymous October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

it will not work if value is 0, correct method is

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

- Rohan May 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

squre root of another number

- raj December 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPow2(unsigned int n){
if(n==0)
return false;
return (n&(n-1))==0;
}

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

If(x && !(x & (x-1))==0)

- Sandy880 January 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if only the left most bit is 1 others 0 only then it is power of 2.. eg..1000 or 0010
x>>1 if 0 continue
else if 1 then the next bit should not be 1.

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

@Jay: Thats interesting point. I was wondering if Negative numbers can be powers of positive number!

- suman April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

take log of that number to the base 2 if the result is an integer then it is a power of 2 else if it is a float it is not a power of 2

- king kong May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ispowerof2(int x)
{
int p = x & -x;
return p == x || p == -x;
}

- Anurag Singh May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ispowerof2(int x)
{
int p = x & -x;
return p == x || p == -x;
}

- Anurag Singh May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

powers of a +ve number 2 will always be +ve

- raped July 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int x = 31;
	int max = 0;
	String s = Integer.toBinaryString(x);
	for(int i=0; i<s.length(); i++){
		max = (int) (max + (Math.pow(2, i)));
	}
	int k = Math.abs((~x & max)+1);
	if(x == k){
		System.out.println("power of 2");
	}
	else{
		System.out.println("not power of 2");
	}

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

int isNonPowerOf2(num)
{
Power=0;
if (num=0) reutrn 0;

while(num>0)
{
power+=(num&0x1);
num = num>>1;
}
return power-1;
}

- samchen2009 February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isNonPowerOf2(num)

Power=0;
    if (num=0) reutrn 0;

    while(num>0)
    {
         power+=(num&0x1);
         num = num>>1;
    }
    return power-1;

- samchen2009 February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just check the last bit of given number, if it is set, its not the power of 2

Bool isPowerof2 = ((A & 0x1) >0) ? (FALSE) : (TRUE);

- Rohan May 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Only last bits value is 1, all other bits of power of 2 like

6 = 0110 = (2^3)*0 + (2^2)*1+ (2^1)*1+ (2^0)*0

- Rohan May 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

2's complement of the of the number and number itself will be equal when the number is the power of two..

- shine May 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
bool val ( int n);
int main ()
{
if(val(1024) == true)
cout << "yes";
else
cout <<"no";

}

bool val(int n)
{

if( n <= 1)
return false;

while(n>1)
{
if(n%2 != 0)
return false;
else
n = n / 2;
}

return true;
}

- Shahid Siddique June 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
{
int number = enter any no;
int quotient = 0;
int mod = 0;
while(number >= 2)
{
mod = number % 2;
quotient = number / 2;

if (mod == 0 && number >= 2)
{
number = quotient; ;
continue;
}
else break;
}
if (mod == 0)
{
Console.WriteLine("number power of 2");
}
else
{
Console.WriteLine("number not power of 2");
}
Console.ReadLine();
}

- AN June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* function to find no is power of 2 or not */

void find(int n)
{
if (n &(n-1))
printf ("Number is not power of 2\n");
else
printf ("Number is Power of 2\n");
}

- Gaurav Kumar Garg December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Count the Number of 1(bits) in the number. only if count is 1, then the number is power of 2. how ever ignore least significant bit

- Gautham January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

bool powerof2(int x)
{
unsigned int i = 0;
int count = 0;
bool p2 = true;
if (x == 1)
{
return false;
}
else
{
while (i < 31)
{
if (x & 1 << i)
{
++count;
if (count > 1)
{
p2 = false;
break;
}
}
i++;
}
}
return p2;
}

int _tmain(int argc, _TCHAR* argv[])
{
bool p2 = powerof2(32);
return 0;
}

- Siddharth More August 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

bool powerof2(int x)
{
unsigned int i = 0;
int count = 0;
bool p2 = true;
if (x == 1)
{
return false;
}
else
{
while (i < 31)
{
if (x & 1 << i)
{
++count;
//// If Count > 1 it is not a power of 2
if (count > 1)
{
p2 = false;
break;
}
}
i++;
}
}
return p2;
}

int _tmain(int argc, _TCHAR* argv[])
{
bool p2 = powerof2(32);
return 0;
}

- Siddharth More August 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To check if the given number is a power of 2 we have to go through the given below algorithm, i.e. we have to first that the & operator with the previous number's ! operation will either return 1 or 0 and then the && comparison operator will return true or false deciding the number is a power of 2 or not.

Implementation:

#include<bits/stdc++.h>
using namspace std;
bool findresult(int n){
return n && (!(n & (n - 1));
}

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

What about negative number? such as -4. -4 is power of 2 too.

- Jay January 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For negative numbers add this line:

bool isPow2(int n){
n=n<0?-n:n;
return !(n & (n-1));
}

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

How? Look at the exponential graph, it never goes below 0 past the x axis.

Also, 2^x = -4, give me x. You can't. The power of any positive integer, such as 2, is always positive. Remember that a number raised to a negative number is just a positive inverse. Therefore, this case is invalid.

- alchu8 May 24, 2012 | Flag


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