I found a Google Interview question on CareerCup
Given a 2D plane, suppose that there are around 6000 points on it. Find a line which passes the most number of points.
Many answers there say this question is hard and involving some kind of special algorithms.
But my view is different and maybe I am wrong.
Here is my thinking:
First I will give a axis system to the 2D plane. Hence, every point will have its unique x and y, i.e., {x, y}
. For simplicity, we can put the axis system's {0, 0}
as the left bottom of the whole plane and therefore every x and y are bigger than 0.
Then I have a theory:
If several points are on the same line, then it must be in one of the following 3 cases:
- their
x
values are the same - their
y
values are the same - their
x/y
ory/x
values are the same. Butx/y
case is the same asy/x
case, so let's just focus onx/y
.
Then I will have 3 hashtables.
The first one (
hashtable-x
) is with key ofx
, the value is the list of points which have the samex
;the second one (
hashtable-y
) is with the key ofy
and the value is the list of points which have the samey
;- the last one (
hashtable-x-y
) is with the key ofx/y
and the value is the list of points which have the samex/y
;
Then I scan the whole 6000 points, for each point, I will get its x
from hashtable-x
, and put the point into the value (a list) of that slot; then I will do similar things to hashtable-y
and hashtable-x-y
.
Finally, i will scan all lists in the 3 hashtables and find the longest list will contains the points of the desired line.
How do you think of my algorithm?
Ok, here is the duplicate, sorry that I didn't find that question before.
What is the most efficient algorithm to find a straight line that goes through most points?