Amazon Interview Question: Given a number find it is one... | Glassdoor.co.in

Interview Question

Software DesignEngineer In Test (SDET) Interview Chennai

Given a number find it is one less than the power of two.

Answer

Interview Answer

15 Answers

6

The question could have been a little more detailed, most of the guys who took up the test could not understand this immediately.

Interview Candidate on 20-Feb-2011
6

Given a number N, XOR it with N+1 = powers of 2 will give all 0s

Anonymous on 25-Feb-2011
0

@Anonymous. That's not true. 3 in binary is 11 and 4 is 100, if you XOR 011 and 100 you will get 111, not 000.

I think you meant to say that all numbers will be 1s.

Anonymous on 12-Mar-2011
3

N xor (N+1) = 2*N + 1

nikki on 04-Jun-2011
4

If the least bit is 1, then it is equal to N less than power of 2

if ( n << 7 )
 cout << "It is one less than power of 2"

For example

1 - 0000 0001
3 - 0000 0011
5 - 0000 0101
7- 0000 0111

Anand on 12-Jul-2011
6

// the power of two is always has the first bit set to1 and rest all would be set to 0. And one less than power of 2 has all the bits set to 1 except the fist bit.
for example 16 = 10000 and 15 = 01111
Now if you do bit operation & on 16 and 15, you get zero.
Hence, below is the code.
public boolean oneLessPowTwo(int num){
boolean b = false;
if(n & n+1 == 0){
b = true;
}
return b;
}

Kalyan Punukollu on 18-Aug-2011
5

the power of two is always has the first bit set to1 and rest all would be set to 0. And one less than power of 2 has all the bits set to 1 except the fist bit.
for example 16 = 10000 and 15 = 01111
Now if you do bit operation & on 16 and 15, you get zero.
Hence, below is the code.

public boolean oneLessPowTwo(int num){
boolean b = false;
if((num & (num+1)) == 0){
b = true;
}
return b;
}

Anonymous on 18-Aug-2011
0

A one line answer to this question is:

   -1 + 2 to the power of -2 - language binary string prefix + length of binary string for N

In Python:

   2**(len(bin(511))-3)-1

Simple one line in any language with a function to convert int to binary string.

Ben on 30-Apr-2012
0

8 anwer

sanjeev kumar on 19-Oct-2012
0

Just another option of doing this

Boolean checkNumber(int inputNum)
{
        int temp = 2 ;
        while(temp < inputNum)
        {
                temp = temp * 2 ;
        }
        if(inputNum == temp-1)
               return True;
        else
              return False;
}

Pramod Vidyagar on 15-May-2015
0

can any1 say it clarly..wt is dz 15+01111??

Anonymous on 11-Jul-2015
0

can any 1 say it clearly?

Anonymous on 11-Jul-2015
0

package amazonExcerise;

import java.util.Arrays;

import org.apache.jasper.tagplugins.jstl.core.ForEach;

public class concatenationArrays {

    public static void main(String[] args) {
        int a[] = { 1, 3, 77, 78, 90 };
        int b[] = { 2, 5, 79, 81 };
        int c[] = new int[9];
        int j = 0;
        for (int i = 0; i 4) {
                c[i] = b[j];
                j++;
            }
            System.out.print(" " + c[i]);
        }
        Arrays.sort(c);
        System.out.println("");
        System.out.println("Sorted Array");
        for (int i : c) {
            System.out.print(" "+i);
        }
        j = 0;
        System.out.println();
        System.out.println("Array A:");
        j = 0;
        for (int i = 0; i = 5) {
                b[j] = c[i];
                System.out.print(" "+b[j]);
                j++;
            }

        }

    }

}

Solution for the second Question: on 02-Nov-2015
0

N is the number so (1<

Anonymous on 16-Dec-2016

One or more comments have been removed.
Please see our Community Guidelines or Terms of Service for more information.

Add Answers or Comments

To comment on this, Sign In or Sign Up.