Google | Phone Screen | Points with the given distance
Anonymous User
2924

Given a set of floating type points, find if there exist any pair of points that distance is <= sqrt(2).
One solution is to partition the grid into multiple unit 1 sqaure:

Maintain a hashMap[id] = {a list of points} where id is identification of unit square on grid ( ex : m rows and n cols can be used as i * n + j)

  1. first round for each point insert into hashMap[id] its belong to ( ex : (a,b) , id = floor(a) * n + floor(b)
  2. iterate throgh each point again, for each point check its unit square and neighbor squares to see if any points having distance <= sqrt(2)

For this solution , I thought the worst case run time is still O(n^2) , for example like below case:

in this case, for each point we still end up check all the rest of point with it
     |..|..|
	--------
     |..|..|

Not sure if anyone know a way for O(N) solution?

Comments (5)