## NVIDIA Interview Question for Software Engineer / Developers

Country: United States

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

float SquareRoot(float num)
{
if(num >= 0)
{
float x = num;
int i;
for(i = 0; i < 20; i ++)
{
x = (((x * x) + num) / (2 * x));
}
return x;
}
}

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

nice sln

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

Can someone please explain the logic?

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

this is exactly Newton's iterative method mentioned below.

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

There is a possibility of overflow.

Better make it :

(x + num/x)/2

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

You can use Newton's iterative method
Ai+1 = 0.5 * (Ai + X/Ai); where i - number of iteration

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

#include<stdio.h>
#include<math.h>

void main()
{
float a,b,c;
printf("\n enther the number square root who wanna found\n");
scanf("%f",&a);
int i,j;
b=a;
for(i=1;i<10;i++)
{
b=0.5*(b+a/b);
}
printf("\nsqrt of %f is %f\n\n",i,a,b);
}

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

This fails for a large number like 1,000,000.

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

I am not sure how it fails for large numbers. The number you used is not particularly larger considering we are using floats here, right? There will be precision loss because of using floats but it shouldn't be too bad, imho.

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

Float sqrt(float num)
{
Float x;
If(num >0)
{
for(i=0;I<20;i++)
{
x=(x*x+num)/(2*x);
}
Return x;
}
}

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

can you explain what is it doing??

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

can u please explain wat is happening here.....

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

float thresh = 0.0000001

float binarysearch(float x, float y, float z)
{
if (x*x < z-thresh && y*y > z+thresh)
{
x = binarysearch((x+y)/2,y,z);
}
else if (x*x > (z+ thresh))
{
x = binarysearch(x/2,x,z);
}
return x;

}

float sqrt(float x)
{
return binarysearch(0,x+1,x);
}

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

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

I don't know what he means by linear search, but binary search is definitely not rubbish.

Comment hidden because of low score. Click to expand.

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.