endless
BAN USER- 0of 0 votes
AnswersThere is a boat on the water. There is a rock in it. At this time, assume the level of the water is L1. Now say, you threw the rock into the water and it reached the bottom. Now what will be the level of the water?
- endless in United States| Report Duplicate | Flag | PURGE
Bank of America Software Engineer / Developer Brain Teasers - 0of 0 votes
AnswersGiven a 2-D array of 8x8 size. Each element contains a randomly generated integer > 0. You need to choose a 3x3 matrix from that. Now an algorithm randomly picks up one element from that matrix.
- endless in United States
1) How will you maximize your chances of picking up the highest number?
2) Assume two players. Player1 selected a 3x3 matrix. Now after seeing that, how can Player2 choose a 3x3 matrix to maximize his chances of winning?| Report Duplicate | Flag | PURGE
Software Engineer / Developer Brain Teasers - 0of 0 votes
AnswersFind out the square root of a double number. The result should be with in +/- 0.00001 of the actual result.
- endless in United States| Report Duplicate | Flag | PURGE
Algorithm
This will work with the binary search approach.
public class Sqrt {
private static final double precision = 0.00001;
/**
* @param args
*/
public static void main(String[] args) {
double number = 0.4923423423;
double min=0, max=0;
for (int i=0;;i++) {
double boundary = Math.pow(5, i);
if (number < boundary * boundary) {
min = max;
max = boundary;
break;
}
max = boundary;
}
System.out.println(getSqrt(number, min, max));
System.out.println(Math.sqrt(number));
}
private static double getSqrt(double num, double min, double max) {
double mid = (min + max)/2;
if (Math.abs(num - mid * mid) <= precision) {
return mid;
}
if (num < (mid * mid)) {
max = mid-precision;
} else if (num > (mid*mid)) {
min = mid+precision;
}
return getSqrt(num, min, max);
}
}
Sounds reasonable to me.
- endless October 16, 2012