## Amazon Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

Please use Newtons method to find the square root... It's relatively easy to code it...

PS: I really don't get why people write all this code in the forum. The followers of this blog can easily code the solution. Let's just discuss solutions and time/space complexities...

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

I totally agree. Working code is not very useful for the users of this forum.

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

Implement the square root method. Don't use the methods. I did one variation of the method. I would like to see if there is another better way to do it.

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

``````public double findSquareRoot(double val){
double DELTA_GAP=0.00001;
double start = 0, end = val, root=val;
while(end-start>DELTA_GAP){
root = (end-start)/2 +start;
if(root * root > val){
end = root;
}else{
start = root;
}

System.out.println(root+" ("+start+","+end+")");
}

return root;
}``````

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

after read sandeep's generic implementation, add a else{} to above square solution.

``````...
double pow = root * root;
if(pow > val){
end = root;
}else if(pow < val){
start = root;
}else{//this branch should be caught, otherwise you cannot get "exact" root, like findSquareRoot(2), you cannot get 2 as result
System.out.println(root+" ("+start+","+end+")");
return root;
}``````

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

Generalizing the above solution further:

``````//Given a value (in double) return its nth root.

public class FindNthRoot {

public static double nthRoot(double number, int n, double tolerance) {
double start = 0;
double end = number;
double root = start + (end - start) / n;

int iterations = 1;
//invariant: (end - start) > tolerance
while ((end - start) > tolerance) {
double pow = nthPower(root, n);

if (pow > number) { // 12 is the number then initial root would be 6 and pow is 36 for n = 2 in which case pow > number
end = root;  // reduce end to root i.e. 6
} else if (pow < number) { // 9 < 12
start = root; // reset start
} else {
return root;
}

root = start + (end - start) / n;
iterations++;
}

System.out.println("start:" + start + ", " + "end: " + end + ", " + "iterations:" + iterations);

return root;
}

private static double nthPower(double num, int n) {
double result = 1;

for (int i = 0; i < n; i++) {
result = result * num;
}

return result;
}

public static void main(String[] args) {
System.out.println(FindNthRoot.nthRoot(36.4345, 2, 0.000010));
System.out.println(FindNthRoot.nthRoot(27.4345, 3, 0.000010));
System.out.println(FindNthRoot.nthRoot(256.4345, 4, 0.000010));
}
}``````

below are the results that I got

start:6.0360919429063795, end: 6.0361006295681, iterations:23
6.03609628623724
start:3.0160069149974285, end: 3.0160132810008373, iterations:25
3.0160090369985646
start:4.00169557736413, end: 4.001703962607476, iterations:38
4.0016976736749665

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

A more generic approach could be based on applying Newton Rapshon's method to find roots of (x^n - number) = 0

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

``````double MySqrt(double f) {
if (f <= 0.0) return 0.0;
double v = f;
for (;;) {
double vv = 0.5 * (v + f / v);
if (vv == v) return v;
v = vv;
}
}``````

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

import java.lang.*;

public class MathDemo {

public static void main(String[] args) {

// get two double numbers numbers
double x = 9;
double y = 25;

// print the square root of these doubles
System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x));
System.out.println("Math.sqrt(" + y + ")=" + Math.sqrt(y));

}
}

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

``````Python solution

print pow(num, 0.5) # num is the input number``````

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.