NVIDIA Interview Question






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

a & (((sizeof(a)*8)-1)<<1)

- Anonymous May 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a>>((sizeof(a)*8)-1)

- jems August 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

input : a
(a & 0x8000)? print("MSB : 1") : print("MSB : 0")

or

printf("MSB : %d", a >> 12);

- Ramesh February 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My Take:
Do right bit shift till there are no bits left.
Let number x has n bits then x>>1 will be O(n).
Any better solution than this ?

- cirus March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming uint32 value.
Do a binary search.
Take masks and AND with the uint32.

Ex: uint16 value = 0000 0101 1100 0010
first step: mask the LSB 8 bit and MSB 8 bits
MSB: 0000 0101
LSB: 1100 0010
MSB > 0 => repeat first step on MSB

MSB: 0000
LSB: 0101
MSB == 0 => repeat first step on LSB

MSB: 01
LSB: 01
MSB > 0 => repeat first step on MSB

MSB: 0
LSB: 1
MSB == 0 => repeat first step on LSB

LSB == single bit. This is the MSB of uint16

The complexity is O(log n)

Please add your thoughts.

- sumit saxena March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to find the msb in smallest no. of steps

Did the question mean to find the position of MSB coz the bits has values 0 and 1. we dont consider the 0 values so did this question needs to find the position of first 1 in MSB.

- sumit saxena March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it was for msb value 1

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

Algo - O(1) but requires precomputed Table

1>Reverse in O(1) using Table
2>Find LSB in O(1)

- JD March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry I got it all wrong .....

- JD March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(a&=(a-1)) ++msb_cnt++;

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

This is equivalent to running a for loop with right shifting the left most bit by 1 each time. Complexity would be O(n) (i.e looping through all the bits)

- sumit saxena March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using asm
MOV A,NUMBER
RAR A
JC
If msb is 1 then JC excute else not

- Ravi March 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

BSR instruction does it in one step.

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

log( number & (number+1))........take base 2

- prakher :) December 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wouldn't MSB be 1 for all numbers except 0...

- Anonymous March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't the MSB just the sign of the number?

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

floor(log2(number))

- dexter January 21, 2014 | 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