## NVIDIA Interview Question

• 0

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

int insert(int x, int a, int b, int k)
{

int y = a;
k= k<<a;
y = y >> (b + 1);
y= y << (b + 1);
y = y|k;
x = y|x;
return(x);
}

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

int insert (int N, int a, int b, int k) {
/* Determine what are needed to be inserted */
int pattern = k & (2<<(b-a))-1; /* Mask the required bits from k */

/* Clear the dst bits and OR with the src pattern */
N = N & ~(2<<(b-a)-1) | pattern;
return N;
}

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

should be:

pattern = (1<<(b-a+1)) - 1;

N = N & ~(pattern<<a) | (K<<a);

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

Hey,

with this example both above algos fail.

N=1011 K=0100 A=1 B=2

So we need to move bits 1 & 2 from K to N. So N becomes 1101

I did something like this

pattern = ~(~0<<(B-A+1))<<A;
N= (N& ~pattern)|( K & pattern);

First statement gives mask from location B to A.
Second just copies required bits from K to N.

For Ex. A=2 ,B=3
~0<<(B-A+1) = 11111100
~(~0<<(B-A+1)) = 00000011
Pattern = ~(~0<<(B-A+1))<<A = 00001100

1.And pattern with K then we get bits needs to be copied .
2. Use the same pattern to remove same bits from N (N &~ pattern).
or above two statements to get result.

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

Gaurav's example seems not right. K seems to be out of range.

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

pattern = (1<<(b+1)-1) & (k<<a)) ;
N & = (1<<(b+1)-1)^ ( 1 <<(a+1)-1))
N | = pattern;

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

pattern = ( ((1<<(b+1) )-1) & (k<<a)) ;
unsigned int x=~0<<a;
N&=x;
N=N|pattern;

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.