Google TPS failed interview
Company: Google
Role: L3/L4 (Software Engineer)
Interview Stage: Phone Screen
Location: unknown
Year: unknown
Original Post Content:
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 side of size from 1 to the rightmost point in that Y — check if a square exists with that size. This might have to be done for all combinations of points on that Y-axis value. The interviewer said that would mean very high time complexity (TC) for so many points.
What I said was O(N2) — how can we optimize it further?
But I was not able to provide an optimized solution. Can anyone help with the same?