Google TPS failed interview
Question:
you are given a list of points in a planar -> could be any number of points with no bounds on the value of x & y.
Find the total number of squares formed by all the points.
points = [
(0,0), (2,0), (2,-2), (0,-2), (4,0),
(4,-4), (0,-4), (4,-2), (2,-4)
] -> This should return 5 squares (4 small + 1 large)
(Top left is (0,0))
x---x---x
x---x---x
x---x---x
I proposed the Brute Force where I sort by Y max to min and X min to Max. So that we start processing top left to bottom right. And for each of side of size from 1 to rightmost point in that y - check if a square exists with that size - Might have to do it for all combinations of point in that Yaxis value. The interviewer said that would mean very high TC for so many points.
What I said was o(n^2) - how can we optimize it further ??
But was not able to provide an optimized solution. Can anyone help with the same ?