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)

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

this is super answer...

- sudhanshu May 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain?

- s May 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

}

- TheWolfe July 16, 2012 | Flag
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);
}

- arun.edarath November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- undefined November 18, 2013 | Flag
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....

- Anonymous May 16, 2012 | Flag Reply
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);
}

- TheWolfe July 16, 2012 | Flag Reply
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....

- Rounak Tibrewal May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- dvyas May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Rounak Tibrewal May 16, 2012 | Flag
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.

- salvo4u May 16, 2012 | Flag Reply
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

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

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

- Rounak Tibrewal May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

(b-a)/2+a;

- deam0n May 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i gues this should work

- codinglearner May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

- Rahul September 13, 2013 | Flag
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 ....

- Anonymous May 19, 2012 | Flag Reply
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)

- PKT May 19, 2012 | Flag Reply
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)

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

Just input some invalid number ( out of range value)

- laki August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- gg January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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

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

- Anonymous August 21, 2014 | Flag Reply
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

- akshaycj47 November 09, 2015 | Flag Reply
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;
}

- swapnilsj August 06, 2016 | Flag Reply
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)

- PKT May 19, 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