Amazon Interview Question
Software Engineer / DevelopersWe cannot test collinearity just based on slope... Consider parallel lines for example...
I think you can just base it on slope because you are using 3 points.. A and B ; A and C.. there is no way they can be parallel. Parallellism will requare 4 points.
Anonymous right, slope is not enough. Points are collinear when they are on the same line, but line equation looks like y = ax + b, where a is slope tangent, b is x coord where line crossing x axis.
So, we'll just need to use pairs (a,b) as a hash key (plus handle vertical lines by some way).
1. Take any two Point - Find thier Slope , x2-x1/y2-y1
2. for the above Points find the Equation of line y=mx+c
if 1 & 2 are same then any point who's line equations is same to 2 is collinear
[Above is bad algo, need to optimise]- as this would require N^2[to calculate the Slope of Line+Line EQ] +N[to find the 3rd Point on same Slope+EQn] computation.
// I need some modification while printing because its printing same set again but logic is fine
import java.util.Arrays;
public class Amazon3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point[] p = new Point[6];
Angle[] angle;
p[0]=new Point(19000, 10000,'A');
p[1]=new Point(18000, 10000,'B');
p[2]=new Point(32000, 10000,'C');
p[3]=new Point(21000, 10000,'D');
p[4]=new Point(1234, 5678,'E');
p[5]=new Point(14000, 10000,'F');
for(int i=0;i<p.length-1;i++)
{
int k = p.length-i-1;
angle = new Angle[k];
for(int j=i+1;j<p.length;j++)
{
angle[j-i-1] = new Angle();
double temp = (p[j].y-p[i].y)/(p[j].x-p[i].x);
angle[j-i-1].angle = Math.atan(temp);
angle[j-i-1].p = p[j];
}
Arrays.sort(angle);
Angle previous = angle[0];
System.out.println();
System.out.println("Colinear Points Set");
System.out.print(p[i].name+","+previous.p.name);
for(int n=1;n<k;n++)
{
if(previous.angle==angle[n].angle)
{
System.out.print(","+angle[n].p.name);
}
else
{
System.out.println();
System.out.println("Colinear Points Set");
System.out.print(p[i].name+","+angle[n].p.name);
}
previous=angle[n];
}
}
}
}
class Point{
double x;
double y;
char name;
Point(double x, double y,char n)
{
this.x =x;
this.y =y;
this.name=n;
}
}
class Angle implements Comparable<Object>{
Point p;
double angle;
public int compareTo(Object o) {
Angle in = (Angle)o;
if(this.angle<in.angle)
return -1;
else if(this.angle==in.angle)
return 0;
else
return 1;
}
}
// This method will return the collinear points set
// pass array of of coordinate points to the the method
public static List<Set<Point>> getCollinearPointSet(Point points []){
int i =0 ;
int j =0 ;
int k = 0;
List <Set<Point>> listCollinearSets = new ArrayList<Set<Point>>();
for ( i=0 ; i < points.length ;i++){
for (j=i+1 ; j < points.length ; j++){
//Skip the combination of points[i] and points[j] if these two points
// are already present in previously calculated collinear set.
if (CollinearUtil.hasPoints(points[i], points[j], listCollinearSets)){
break;
}
Set <Point>collinearPoints = new HashSet<Point>();
for (k = j+1 ; k< points.length ; k++){
if (CollinearUtil.isCollinear(points[i],points[j],points[k])){
collinearPoints.add(points[i]);
collinearPoints.add(points[j]);
collinearPoints.add(points[k]);
}
}
if (!collinearPoints.isEmpty()) listCollinearSets.add(collinearPoints); ;
}
}
return listCollinearSets;
}
// This method will check if given 3 points are collinear.
public static boolean isCollinear(Point p1, Point p2 ,Point p3){
if(((p1.y -p2.y) * p3.x + (p2.x-p1.x)*p3.y + (p1.x*p2.y -p2.x*p1.y))==0){
return true;
}
return false;
}
public static boolean hasPoints(Point p1 ,Point p2 , List<Set<Point>> lisCollinerSets){
for (Set<Point> points : lisCollinerSets) {
if (points.contains(p1) ||points.contains(p2)) return true;
}
return false;
}
// This method will return the collinear points set
// pass array of of coordinate points to the the method
public static List<Set<Point>> getCollinearPointSet(Point points []){
int i =0 ;
int j =0 ;
int k = 0;
List <Set<Point>> listCollinearSets = new ArrayList<Set<Point>>();
for ( i=0 ; i < points.length ;i++){
for (j=i+1 ; j < points.length ; j++){
//Skip the combination of points[i] and points[j] if these two points
// are already present in previously calculated collinear set.
if (CollinearUtil.hasPoints(points[i], points[j], listCollinearSets)){
break;
}
Set <Point>collinearPoints = new HashSet<Point>();
for (k = j+1 ; k< points.length ; k++){
if (CollinearUtil.isCollinear(points[i],points[j],points[k])){
collinearPoints.add(points[i]);
collinearPoints.add(points[j]);
collinearPoints.add(points[k]);
}
}
if (!collinearPoints.isEmpty()) listCollinearSets.add(collinearPoints); ;
}
}
return listCollinearSets;
}
// This method will check if given 3 points are collinear.
public static boolean isCollinear(Point p1, Point p2 ,Point p3){
if(((p1.y -p2.y) * p3.x + (p2.x-p1.x)*p3.y + (p1.x*p2.y -p2.x*p1.y))==0){
return true;
}
return false;
}
public static boolean hasPoints(Point p1 ,Point p2 , List<Set<Point>> lisCollinerSets){
for (Set<Point> points : lisCollinerSets) {
if (points.contains(p1) ||points.contains(p2)) return true;
}
return false;
}
public class Point {
public int x ;
public int y ;
public String name ;
Point (String name ,int x,int y){
this.name = name ;
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point p =(Point)obj ;
if (this.name.equals(p.name)) return true;
if ((this.x == p.x) && (this.y==p.y)) return true ;
return false ;
}
@Override
public String toString() {
StringBuffer coordinates = new StringBuffer();
return coordinates.append(this.name).append(": (").append(this.x).append(",").append(this.y).append(")").toString();
}
For every 2 points, find the slope of the points and store in the hash table. Now go through the hash table and print all the sets that are having the same key value(i.e
- Messi April 20, 2010slope). The complexity will be O(N^2).
Make sure the value of the hashtable is represented as a set, to avoid duplicates.
Idea being, if two points are having the some slope, then the third point which is collinear will also have the same slope with either of the two points.
Please correct me if I am wrong.