Bloomberg LP Interview Question for Software Engineer / Developers Financial Software Developers






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

"Find if a num is a power of 2 as fast as possible"

{
if((num & (num-1))==0)
num is a power of 2
else
num is nota a power of 2
}

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

check id num=0 ??

- Anonymous October 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How abt

if((num%2)!=0)
num is not power of 2
else
num is power of 2

As 2 to the power x is divisible by 2 and reminder is 0

Correct me if m wrong ....

- ;) February 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

';)' is wrong because your progam will output all even numbers to be a power of 2 (which is obviously wrong)..

- Pavan February 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Phenom

the if condition should be:

if(num && (num & (num-1))==0) // to handle the case where input is 0

- garry October 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

0 can't be expressed as power of 2, right?

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

0 can't be expressed as power of 2, right?

- anony February 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will fail when num = 1

- Anonymous October 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 is 2 ^ 0

- Anonymous October 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(a&(a-1)==0)
power of 2...

- karthik November 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if a was a power of 2 ,a xor a-1 would be 1 always

- ran November 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

karthik answer is correct.

- Avinash November 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

it is incorrect.
1010 & 1001 is not 0

- brahmananda reddy November 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ignore that above comment

- Anonymous November 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

10 and 9 are not powers of 2

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

so many nuts

- Anonymous October 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a xor a-1 is not 1.. rather, it is 1111... :)
This implementation leads to more conditional checking.. as the result would be 000..111....

- Teja November 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Karthik's answer is incorrect as it will mark 0 as a power of two
So the correct version is

unsigned int v; // we want to see if v is a power of 2
bool f;         // the result goes here 

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

- sudipta November 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What does it mean power of 2? Does it mean its square root have to be an integer number?

- stian November 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Woudl'nt any number which is a power of 2 have just a single 1 in its bit representation? How about we count the no. of bits set?

- Anonymous November 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<i>How about we count the no. of bits set?</i>

optimal code: (Knuth's algo)

int cnt = 0;

while ( n &= (n-1) && ++cnt );

cout << cnt ;

- Anonymous November 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

shift left most bit out, see if it's zero?

- Anonymous November 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i & (--i) != i

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

first answer (num & (num-1))==0) correct
i & (--i) != i fails.

- henry January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, why are we ANDing the number ( num ) with ( num -1 ) ? Although it would lead to a correct solution, instead we can check whether the binary representation of the number contains "1" at the MSB and zeroes at all other places

- Mandar January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mandar, you're so fucking dumb. a 1 at the doesn't hold for any powers of two other than 1. what about 4, is there a 0 in MSB?

What you need to check is the following as one of the posts above mentioned.

if(num != 0 && (num & (num - 1) == 0)
  return true;
else
  return false;

- augustine.mathew February 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are terribly dumb!

Mandar said, chack if the MSB is 1 and everything else is 0.

For 4, the binary representation is 100 and the MSB is 1. What a loser you are....

- Mahesh February 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be you need to get fucking conventions and terminology right. Also, learn some English while you are at it. As far conventions go the MSB is the 32nd bit on 32 bit machine, you fag, i.e. highest order bit.

May you should wiki "Most significant bit".

- Anonymous February 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Say, x = number; and power is n

2^n = x <<<we have to find "n" in this.
Take log on both side.
n = log(x)/log(2);

if n is an integer, then x can be expressed as 2^n, otherwise not.

You can check for x=0 explicitly.

- Anonymous February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Check no of bits set because in a no which is a power of 2 only the MSB is set.

void check ( int no ) {
int temp = 0x00000001, cnt ;
if ( no ) {
for ( i = 0; i < 32; i++ ) {
temp = temp << i;
if ( temp & no != 0 )
cnt++ ;
}
}
if( cnt == 1 )
printf (" No. is a multiple \n");
else
printf("not a multiple\n");
}

- chetan May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey42535" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}

</pre><pre title="CodeMonkey42535" input="yes">1
2
10
42
11

</pre>

- nirmalah04 June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool TestPowerOfTwo(int n){
if((n&(n-1))==0)return 1;
else return 0;
}

you guys are good. this works perfectly

- Anonymous June 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What a wonderful question!
Bad answers

If a number is 2^n then it's binary form only have one 1
others are all zero!

- macdo@live.cn February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static boolean isPower2(int number){
		if(number == number - (number & (number - 1)) && number != 1){
			return true;
		}
		else return false;
	}

- Yiran Qin March 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

tested to function well for positive integers

- Yiran Qin March 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for both negative and positive integers


static boolean isPower2(int number){
		number = (number < 0)? (0 - number) : number;
		if(number == number - (number & (number - 1)) && number != 1){
			return true;
		}
		else return false;
	}

- Yiran Qin March 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPowerOfTwo(int num) {
    if (num == 2 || num == 1) return true;
    int i = 2;
    while (i < num) {
      if (i * 2 == num) return true;
      i = i * 2;
    }
    return false;
  }

- Zhengyang.Feng2011 March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As has been stated numerous times already, simply &ing the number and the number minus one will get you the correct answer (as long as you also check for the zero case).

The following code also works. It is less elegant, but it more exhaustively goes through the byte without being too cumbersome.

int count = 0;
  for(int i = 0; i<sizeof(aInt); i++){
    
    count+=aInt & 1;
    if(count > 1)
      return false;
    
    aInt>>=1;
  }
  printf("%d\n", count);
  
  return true;

- Brandon May 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPowOf2(int n)
{
	if(n && !(n & (n-1)))
		return 1;
	return 0;

}

- sumit October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_power_of_2(int n)
{
  if (n < 0) n = -n;
  return (n & -n) == n; // extract the rightmost bit 1
}

- yaojingguo December 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int v=0;v<2050;v++)
if(Integer.toBinaryString(v-1).matches("1+"))
{
System.out.println(Integer.toBinaryString(v)+" "+v);
}

- altF2+r February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
for(int v=0;v<2050;v++)
if(Integer.toBinaryString(v-1).matches("1+"))
{
System.out.println(Integer.toBinaryString(v)+" "+v);
}
}}

- altF2+r February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <math.h>
using namespace std;




int main()
{

    int n;

    cin >> n;

    double resF= log10(n)/log10(2);
    int resI = log10(n)/log10(2);

    double resF2 = (double) resI;

    if (resF == resF2)
    {
        cout << "n is of power 2" << endl;
    }
    else
    {
        cout << "n is NOT of power 2" << endl;
    }

    return 0;
}

- MMSHX March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <math.h>
using namespace std;




int main()
{

    int n;

    cin >> n;

    double resF= log10(n)/log10(2);
    int resI = log10(n)/log10(2);

    double resF2 = (double) resI;

    if (resF == resF2)
    {
        cout << "n is of power 2" << endl;
    }
    else
    {
        cout << "n is NOT of power 2" << endl;
    }

    return 0;
}

- MMSHX March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
#include <iostream>
#include <math.h>
using namespace std;

int main()
{

int n;

cin >> n;

double resF= log10(n)/log10(2);
int resI = log10(n)/log10(2);

double resF2 = (double) resI;

if (resF == resF2)
{
cout << "n is of power 2" << endl;
}
else
{
cout << "n is NOT of power 2" << endl;
}

return 0;
}
}}

- MMSHX March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <math.h>
using namespace std;

int main()
{

    int n;

    cin >> n;

    double resF= log10(n)/log10(2);
    int resI = log10(n)/log10(2);

    double resF2 = (double) resI;

    if (resF == resF2)
    {
        cout << "n is of power 2" << endl;
    }
    else
    {
        cout << "n is NOT of power 2" << endl;
    }

    return 0;
}

- MMSHX March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double resF= log10(n)/log10(2);
    int resI = log10(n)/log10(2);

    double resF2 = (double) resI;

    if (resF == resF2)
    {
        cout << "n is of power 2" << endl;
    }

- MMSHX March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double resF= log10(n)/log10(2);
int resI = log10(n)/log10(2);

double resF2 = (double) resI;

if (resF == resF2)
{
cout << "n is of power 2" << endl;
}

- MMSHX March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the most efficient method to check whether a given number is a power of two or not we will check for the & of the number and the number before itself and then for fulfilling the condition of 0 we have to check for && operation.

Implementation:

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

}

- swapnilkant11 June 19, 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