Ebay Interview Question for Software Engineer / Developers


Team: cloud
Country: United States
Interview Type: Phone Interview




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

A simple binary search method.

The idea is f(x) = x^2 is monotonically increasing function.

So to find sqrt of Y. We need to find an x such that x * x = Y.

We start with initial guesses 0 and Y and keep bisecting the range Y in which we know the sqrt cannot lie.

const double ERROR = 0.000000001;

double findsqrt( double t )
{
	double low = 0;
	double high = t;

	while ( (high-low)  > ERROR )
	{	
		double mid = ( low + high ) /  2.0;
		if ( (mid*mid) > t )
		{
			high = mid;
		}
		else
		{
			low = mid;
		}
	}
	return low;
}

- Anonymous October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

Look for Newton–Raphson method

- Anonymous October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

why we are taking double high = t; in place of double high = t/2;

- Anonymous October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

the sum of a sequence of odd numbers gives you the sequence for the perfect squares
1=1
1+3=4
1+3+5=9
1+3+5+7=16

- HorsePee October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not assuming. Its just a useful property that I felt I should add

- HorsePee October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a very interesting pattern.

- Yo October 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(n+1)^2 = n^2 + (2n + 1), and 2n +1 is the n+1-th odd number, hence the relationship. The square root might not be an integer though, so we need a bit more here.

- eugene.yarovoi October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

double target = 100;

double n = 0.001;

while (n*n < target) {
n += 0.001;
}

System.out.println(n);

- Amol January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double target = 100;

double n = 0.001;

while (n*n < target) {
n += 0.001;
}

System.out.println(n);

- Amol January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java: [based on binary search method]

double FindSquareRoot(double n)
	{
		double sr;
		final double ERROR = 0.00001;
		return FindRootByBinarySearch(n, 0, n/2, ERROR);
	}

	double FindRootByBinarySearch(double n, double low, double high, double ERROR)
	{
		if ((high-low) < ERROR)
			return low;
		double mid = (high+low)/2;
		if ((mid*mid)>n)
			return FindRootByBinarySearch(n, low, mid, ERROR);
		else
			return FindRootByBinarySearch(n, mid, high, ERROR);
	}

- rajarshi129 October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just code this algorithm
basic-mathematics.com/square-root-algorithm.html

- Drishti March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The basic idea here is using Newton Raphson:
y = x^2

y' = 2x

x f(x) f'(x) x' = x - f(x)/f'(x)

we use x' to replace x in each iteration until the difference value is smaller than a constant (usually 0.0000001)

At last, we will get an approximate result for x (x = sqrt(y) )

- Kevin March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be done in O(1) me thinks (for a range of inputs numbers for our application).

Use Taylor's polynomial theorem.

Find polynomial expansion of sqrt(1+x) about x=0.
For the required range of inputs, design the expansion to have degree K such that the error is within required precision for the range of inputs.

Now you have a polynomial p(x) of degree K to use for your application.
{{
sqrt(num)
{
// evaluate the special polynomial at x= num -1
// above can be done with O(K) operations. K is fixed.
return whatever you got ;
}
}}}

- S O U N D W A V E October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sqrt(num)
{
// evaluate the special polynomial at x= num -1
// above can be done with O(K) operations. K is fixed.
return whatever you got ;
}

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

a

- Anonymous April 25, 2013 | 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