## NVIDIA Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
17
of 19 vote

int avg = (a/2) + (b/2) + ((a%2 + b%2)/2)

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

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

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

``````int computeAvg(int a, int b)
{
return (a >> 1 + b >> 1 + (a&1 + b&1) >> 1);``````

}

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

Add "1" only when both end with a bit 1 (aka both are odd)
int computeAvg(int a, int b)
{
return (a >> 1 + b >> 1 + (a&b) &1);
}

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

@TheWolfe: That is good but it doesn't work because of braces not present.Slight modification makes it work.

``(((a >> 1) + (b >> 1)) + (((a&1)+ (b&1))>>1));``

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

suppose a and b are int....
and a and b = (2^31)-1;

so (a+b) will cause overflow and hence (a+b)/2 wnt work

so we need to use (a/2) + (b/2) keeping in mind if they are even or odd so that the fractional part is not lost....

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

``````int computeAvg(int a, int b)
{
return (a >> 1 + b >> 1 + (a&1 + b&1) >> 1);
}``````

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

suppose a and b are int....
and a and b = (2^31)-1;

so (a+b) will cause overflow and hence (a+b)/2 wnt work

so we need to use (a/2) + (b/2) keeping in mind if they are even or odd so that the fractional part is not lost....

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

you'r somewhat correct... can you elaborate on the odd/even part ??

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

if both are odd ans=(a/2)+(b/2)+1;
else its ans=(a/2)+(b/2);

P.S:only if you consider only the integral part like 9/2=4

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

i think if a+b flows out of integer bounds then the above formula might fail.
This could work:
int x=a/2;
int y=b/2
print --->x+y
Here u never calculate a+b.

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

You can use :

a-(a-b)/2

This wont be affected by odd or even

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

i think it wnt wrk if a=(2^31)-1 and b= -(2^31)....which again causes overflow

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

(b-a)/2+a;

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

i gues this should work

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

For +ve values and some -ve , but will fail for :

b = 2^31-1, a = -2^31 causing (b-a) to overflow!

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

But, if a and b both are integers, then result is neva accurate using (a+b)/2 , hence
y to bother for (a/2)+(b/2) wudn't work ....

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

1)
In case
a=0 (as 0 is also a number)
b=0
(a+b)/2 = 0/2 will be fail

sol: if both number are 0 result =0
-------------------------
2)
As average formula (a+b)/2 does not mention that return type is int or float
in case
int average (int a,int b)
{
return (a+b)/2 ;
}

for single odd numbers this function will be fail
-------------------------------
3)If
float average (int a,int b)
{
return (a+b)/2 ;
}

number out of range (a+b) is the only issue... for which sol is already mentioned above in the top.... (a/2 + b/2)

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

1)
In case
a=0 (as 0 is also a number)
b=0
(a+b)/2 = 0/2 will be fail

sol: if both number are 0 result =0
-------------------------
2)
As average formula (a+b)/2 does not mention that return type is int or float
in case
int average (int a,int b)
{
return (a+b)/2 ;
}

for single odd numbers this function will be fail
-------------------------------
3)If
float average (int a,int b)
{
return (a+b)/2 ;
}

number out of range (a+b) is the only issue... for which sol is already mentioned above in the top.... (a/2 + b/2)

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

Just input some invalid number ( out of range value)

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

return (a&b) + ((a^b)>>1)

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

a&b + ((a^b)>>1)

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

a /2 + b /2 + (a % 2 + b % 2) /2

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

It won't work when a and b are huge numbers. It will cause overflow.
The correct solution will be: a + (b - a) / 2

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

``````int avgFunction(int a, int b){
int ans = (a >> 1) + (b >> 1) + ((a & b) & 1);
return ans;
}``````

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

1)
In case
a=0 (as 0 is also a number)
b=0
(a+b)/2 = 0/2 will be fail

sol: if both number are 0 result =0
-------------------------
2)
As average formula (a+b)/2 does not mention that return type is int or float
in case
int average (int a,int b)
{
return (a+b)/2 ;
}

for single odd numbers this function will be fail
-------------------------------
3)If
float average (int a,int b)
{
return (a+b)/2 ;
}

number out of range (a+b) is the only issue... for which sol is already mentioned above in the top.... (a/2 + b/2)

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.