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

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

That is a cool solution.

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

cool

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

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

nice sol!!!

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

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

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

}

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
}

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

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.

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.

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

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

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

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...

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

we should not use any inbuilt function.

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

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

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

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

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

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

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

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

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

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 }

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

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;``````

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");``````

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

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

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

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

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

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

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

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

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

``a/b?a:b``

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.