## Google Interview Question

Software Engineers**Country:**United States

**Interview Type:**Phone Interview

The idea is to map the 2d points to some sort of fixed reference and only compare where it make sense, instead of comparing each point with all the other point, which makes it a N2 algorithm. So here is my take on this. Divide the entire grid into a chunk of k x k grid squares.

e.g. 1st squre(0,0)(0,k)(k,0)(k,k)

2nd squre (0,k)(k,k)(0,2k)(k.2k)

Then iterate over each point and assign a parent grid

CEIL(i/k) * k, CEIL(j/k)*k

Once we have the squres assigned, iterate over the points which belongs only to adjacent 8 sqaures. Rest will lay out of bound for any 2d points inside this grid. Then like minimum spanning tree algorithm, keep chaining the points.

You can find this in O(N) approach. Find the min from the array and the max from the array.

Now you should have your buckets like this:

bucket1: [min, min + k]

bucket2: [min + k + 1, min + k + 1 + k]

..

bucketN/K: [max - k,max]

Now, you start iteration over the array and find out which bucket the current number belongs to and add it in the corresponding bucket.

When the iteration is over, just check for those buckets whose count of items inserted is greater then 1.

gjgjg

- khkh December 02, 2019