Goldman Sachs Interview Question
Analystsmay be this shud do ..
consider if sq1 is in 1st quad and sq2 is in 3rd quad ..
now reflect the sq2 on x axis so that it falls on 2nd quad... now reflect it again on 1st quad ... so now both squares will be in 1st quad ..now adjust them so that they both lie on each other ,, using rotations ,, displacements .. now draw a line at n/2 .. after cutting .. reverse the process to bring back to quad3..
public class Square {
public double left;
public double top;
public double bottom;
public double right;
public Square(double left, double top, double size) {
this.left = left;
this.top = top;
this.bottom = top + size;
this.right = left + size;
}
public Point middle() {
return new Point((this.left + this.right) / 2,
(this.top + this.bottom) / 2);
}
public Line cut(Square other) {
Point middle_s = this.middle();
Point middle_t = other.middle();
if (middle_s == middle_t) {
return new Line(new Point(left, top),
new Point(right, bottom));
} else {
return new Line(middle_s, middle_t);
}
}
}
Any Line passing the centers of the two squares will cut them in halves
- mkk4u December 08, 2006