NVIDIA Interview Question for Software Engineer / Developers






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

well ths soultion will be like
1) let the number be A = 10101111
2) A1 = A & 10101010 = 10101010
3) A2 = A & 01010101 = 00000101
4) A1 = A1 >> 1 = 01010101
5) A2 = A2 << 1 = 00001010
6) Answer = A1 | A2 = 01011111

so u require 5 instructions

- Deepak Sharma March 30, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

XOR with 1's

- goku December 03, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

XOR with 1's will not work. If num=11011010, XOR will give 00100101 instead of 11100101.

My solution is:

int reverseOddEvenBits (int src)
{
int rst;

/* 0xaa = 10101010, 0x55 = 01010101 */
rst = ((src & 0xaa) » 1) | ((src & 0x55) « 1);

return rst;

}

- rogerRabbit December 09, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

need only one ~ (not) operation.
Eg. A= 0x10101111;
B = ~A = 0x01010000;

- RichmondFox June 07, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We need to swap even and odd bits .
for A=0x10101111;
Ans should be B= 0x01011111;
Deepak's answer is correct !!

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

here we r dng either rotate left with carrry or rotate with right
so if we consider rotate left
a1=num & 0x80000;//get the msb
a=a<<1; //where a is number //on which we r apllying
a=a |a1;
so we required here three instructions

- darshan June 18, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

not operator would not work in this case.

for example,

A=00000000
~A = 11111111

where swap(A) should return 00000000

- nunbit romance June 19, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it needs 3 instructions:
shl $0x1, $A
jnc 1f
or 0x1, $A
1f:

- noname June 20, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you elaborate with an example?

- Anonymous June 20, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

e.g A = 10101010

What the instruction "shl $0x1, $A" does is as follows:
CF A
1 <-- 01010100 <-- 0

Now, CFlag is 1.

The logic of the rest 2 instructions is as follows:
If CF is 0, does nothing; Otherwise, set the last bit of A to 1.
In another word, these two instuctions set the last bit of A as the value of CF.

In this example, A will be set as 01010101

Another example: A = 01010111
The first instruction does:
CF A
0 <-- 10101110 <-- 0

Because CF is 0, it will do nothing. The result is already what we want.


Similarly, you can use the instruction "SHR", too.

- noname June 20, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry that there are two typos in the codes. The correct one should be:

shl $0x1, $A
jnc 1f
or $0x1, $A
1:


And it is gcc assembler syntax. For the intel assembler syntax, it should be
shl A, 1
jnc label1
or A, 1
label1:

- noname June 20, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is problem in the piece of code u've posted

say my number is: num = 00111001(binary) which after swapping odd n even bits shud be 00110110

shl num,1 wud make it 01110010
and as there is no carry generated this wud be the result generated and that is wrong.

swapping odd n even bits wud take 5 instuctions atleast. as posted by deepak sharma

- Naveen M R June 21, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

However, if you write deepak shrma's code in a asembly language, it will take 6 instructions instead of 5.

- Anonymous June 21, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You are right. I am cofused.

- noname June 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int swapBits(unsigned x)
{
return ((x & 0xAAAAAAAA)>>1) | ((x<<0x55555555))
}

- Anonymous February 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

well you mean
int swapBits(unsigned x)
{
return ( ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1) );
}

- wax February 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

5 makes sense

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

Isn't this just a barrel shifter?

// n is the thing you want to change

result = (n << 1) | ((n >> 31) & 1)

e.g.
int n = 1110 00...00 0101
n << 1 = 1100 00...00 1010
((n >> 31) & 1) = 1
result = 1100 00...00 1011

- anon March 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
char temp;
char *str=(char *)malloc(100);
printf("Enter Number in Binary form");
gets(str);
for(int i=0;i<strlen(str);i=i+2)
{
temp=*(str+i);
*(str+i)=*(str+i+1);
*(str+i+1)=temp;
}
printf("Output is");
printf("%s",str);
getch();
}

- guptashubham007 August 28, 2012 | 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