NVIDIA Interview Question






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);
}

- jvt August 09, 2007 | Flag Reply
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;
}

- rogerRabbit January 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

should be:

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

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

- Anonymous April 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Gaurav April 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- billdoors May 12, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about something like this:

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

- billdoors May 12, 2008 | Flag Reply
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;

- Anonymous August 19, 2009 | 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