## Qualcomm Interview Question

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

clear(int x,int n)
{
int k=1;
return x;
}

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

k = 1;
for(int i = 0; i < n; i++)
k<<1;
m = k ^ 0;
input = m & input;

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

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

In the above solution, in place of "mask | = 1<<(n-1);" we can simply keep "mask = <<(n-1);". There is no need of using |=. Please correct me if I am wrong.

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

In the above solution, in place of "mask | = 1<<(n-1);" we can simply keep "mask = 1<<(n-1);". There is no need of using |=. Please correct me if I am wrong.

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

return nInput&(~(1<<(n-1)));

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

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

``````int clear(int n, int num)
{
num = 1<<(n-1) ^ num;
return num;
}``````

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

Sorry, I also wanted to add the condition to check if the bit=0, then there's no need to clear.
int clear(int n, int num)
{
int newnum = 1<<(n-1) ^ num;
if(num & (1<<(n-1)) == 0)
return num;
return newnum;
}

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

All of these solutions are of order n where n is the bit to be cleared. This can, however, be done in constant time with a lookup table. Build a table (an array) that has a preset bitmask for each bit in the integer. The 'Clear' function then looks up the mask using the value of n passed in. The mask is then applied and the modified value of x returned.

O(n) is too much for a function like this that may be used many times. The lookup table solution is O(1).

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

your solution looks good when we use it many times.

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

int clear_n(int num,int n)
{
return (num = num&(~(1<<(n-1)));
}

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

n ^ (1<<(n-1))

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

Java Code:

``````public static int clearNthBit(int num, int nthBit){
if(num == 0)
return num;

int k = 1<<(nthBit-1);
if((num & k) >= 1){
return num ^ k; //bit is set clear the bit
} else {
return num; //bit is clear
}
}``````

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

return ip&(~0-pow(2,n));

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

return ip&(~0-pow(2,n));

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

num &= (~0<<n)

num is integer
n bit want to disable

please correct me if im wrong

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

``````x=number/(10^n); //quotient
number=number-x*(10^n);``````

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

``````k=size(number) - n;
array x[k];
x[k]=number/(10^n);
number=number-*(10^n);``````

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

import java.lang.*;
import java.io.*;
import java util.*;
class toggle_n{
int n,b=1;

System.out.println("enter an number");
Scanner sc=new Scanner();
int a=sc.nextInt();

System.out.println("enter position to be toggle");
n=sc.nextInt();

b=b<<(n-1);
a=a^b;

System.out.println("number after toggling nth bit");
System.out.println(a);

}

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

``````//question 5 of java queston is to toggle nth bit
import java.io.*;
import java.util.*;
import java.math.*;

class toggle{

public int i,j;
int index=0,id=0;
int binary[],binary2[];

void toggling_nbit()throws IOException
{
System.out.println("ENTER A NUMBER ");
String s;
String p=" ";
int a=Integer.parseInt(s);
binary = new int[40];
tobinary(a);

System.out.println( );
System.out.println("WHICH BIT U WANT TO TOGGLE");
int n=Integer.parseInt(s);
int b=1;
int c=0;
System.out.println( );
for(i=1;i<=n-1;i++)
{       System.out.println( );
System.out.println("SHIFT FACTOR" );
System.out.println(c=(b<<i));
}

binary2=new int[40];
id = 0;

while(c> 0)
{
binary2[id++] = c%2;
c = c/2;
}

/*for( i = id-1;i >= 0;i--)
{
System.out.print(binary2[i]);
}*/
System.out.println( );
xor(binary,binary2);

}//toggling function closed

void tobinary(int a)
{
while(a > 0)
{
binary[index++]= a%2;
a=a/2;
}
System.out.println(" BINARY VALUE OF INPUT" );

for( i = index-1;i >= 0;i--)
{
System.out.print(binary[i]);
}

}//to binary function vclosed

void xor(int binary[],int binary2[])
{
System.out.println("VALUE AFTER TOGGLE");
for(i=0;i<=index-1;i++)
{
if(binary[i]==binary2[i])
{binary[i]=0;}

else
{ binary[i]=1;}
}

for( i = index-1;i >= 0;i--){        //binary of input
System.out.print(binary[i]);
}

}

}//class closed

class toggle_n{
public static void main(String args[]) throws IOException{
toggle t =new toggle( );
t.toggling_nbit( );
}//main closed

}//class closed``````

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.