NVIDIA Interview Question for Software Engineer / Developers






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

Hmm... its basically how a multiplexer works . suppose 2 numbers are A and B .
Perform the operation A-B and extract the MSB .let it be k. Now if k =1 then A>B else B>A (2's complement)..

return (1-k)*A+k*B. This will alyways return the maximum of A&B

- marly April 14, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is a cool solution.

- vodangkhoa April 15, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool

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

a little improvement:

int s = (A - B) >> 31;
return (A & ~s) | (B & s);

- asm December 02, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice sol!!!

- duck January 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the best solution, just wanted to add one more information.

I guess in some machines, while shifting the bits the sign is also saved. So it should be made unsigned int to make the code work in all machines.

So the working code what I have.

int a,b=0;
	a = 7;
	b = 9;
	int diff = (unsigned int)(a-b) >> 31;
	printf("\nDiff : %d\n",diff);
	int sum = (1-diff)*a + (diff*b);
	printf("\n Max of %d and %d is : %d\n",a,b,sum);

- JP June 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

max_of_two_int(int a , int b)
{

int max[2];
int pos;
max[0] = a ;
max[1] = b;

pos = ((a-b) >> ((sizeof(int)*8 - 1))) & 1;


printf("\nmax = %d \n",max[pos] );

}

- brijesh kumar jaiswal April 18, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's only one line code if we can use ternary operator.

maxint(int a, int b){
return (a-b >> (sizeof(int)*8 - 1)) ? b:a
}

- jerrychen July 15, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Marley,

I am a little unclear here.
for eg : A = 1 and B = 2

A-B in Two's compliment is 11

here MSB = 1 , by ur logic if k = 1 then A > B which seems incorrect. I think it should be the other way ? B is greater than A if MSB of A-B = 1. Please correct if I am wrong.

- vasu July 26, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max(int x,int y)
{
return(x-((x-y)&(-(x<y))));
}

However the comparison operator is used here.

- sgfault August 16, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return (A+B+abs(A-B))/2

- Anonymous October 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I also thought of this one, but I think that there might be a problem with it -- we don't know whether abs() uses a conditional in its implementation...

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

we should not use any inbuilt function.

- Anonymous March 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

#define MAX(a,b) \
{\
int m = (a - b) & 0x80000000;\
int s = 0;\
s = (m>>31) & 0x1;\
printf("max is %d", (1-s)*a + s*b);\
}

int main(void)
{
MAX(15, 11);
return 0;
}

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

@technoviking ur solution is just a copy of the first solution given by marley....plz dont repeat the answer just by wrapping it in a different way....avoid syntactic sugar coatingsss, kick ur ass

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

int foo(int x, int y)
{
return ( x-(((x-y)>>(WORDBITS-1))&(x-y)) );
}

- snowgoose May 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
int i=12,j=11;
int x=(i-j+abs(i-j))?i:j;
printf("%d",x);
getch();
}

- sharad kumar August 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the answer to this question:
interviewcodesnippets.com/?p=186

- Moe July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is something called shortcircuiting in C.
If we can use that:
((x>y)&&k=x);
((y>x)&&k=y);

- tejasudha November 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Very Simple :)

int a=x=100;
int b=y=500;
int max;

for (i=0; i<x || i<y; i++ )
{
x++;
y++;
}

max = (x+y-(a+b))/2;
cout<<max;

ex:
100 , 500

max = (x+y-(a+b))/2;
max=(600+1000-(100+500))/2
max=(1600-(600))/2
max=(1000)/2
max=500

- PKT March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my fault.. i used '<' in for loop:
correcting it:

int a=x=100;
int b=y=500;
int sum=x+y=600;
int max;
int flag1=0,flag2=0,flag3=0;

while(!flag3)
{
a--;
b--;

x++;
y++;

while(!a)
{
flag1=1;
break;
}

while(!b)
{
flag2=1;
break;
}

while(flag1 && flag2)
{
flag3=1;
break;
}

}

max = (x+y-(sum))/2;
cout<<max;

ex:
100 , 500

max = (x+y-(sum))/2;
max=(600+1000-(100+500))/2
max=(1600-(600))/2
max=(1000)/2
max=500

- PKT March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 int getMax(int a, int b) {
2 int c = a - b;
3 int k = (c >> 31) & 0x1;
4 int max = a - k * c;
5 return max;
6 }

- kim July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max(int x,int y)
{
return y+((x-y)& ~(x-y)>>31);
}

- Akhil November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double mymax (int a, int b)
{ 
double result;
result = (Math.Sqrt(a * a + b * b - 2 * a * b) + a + b) / 2;
return result;

- Uday July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another solution can be :

int a,b;
while(a/b)
	printf("a is greater b\n");
while(b/a)
	printf("b is greater a");

- nireesha916 October 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max(int a,int b)
{return a*b/b+b/a*a;}

- Little Ding November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should be a*(b/a)+b*(a/b)

- Anonymous November 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work if a or b is not a complete multiple of the other number.

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

int max = a * (Math.abs(b-a) + a-b)/(2*(a-b)) + b * (Math.abs(a-b) + b-a)/(2*(b-a));

- Aziz January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(a+b + abs(a-b))/2

- Anonymous August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a/b?a:b

- theone June 06, 2015 | 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