Google Interview Question
Software EngineersCountry: United States
Interview Type: Phone Interview
You can not do better than N^2 but some quick optimizations can be done which are also practical during the interview. Like one I am doing below is that list of points is sorted based on their point.x values and comparing each point to the other points from left to right, if point2.x - point1.x > k that there is no need to compare any other values in rest of the list as all the other values will have x values equal to, or bigger than point2.x value. Note: The code below is not tested
List<List<Point>> getSets(Point [] points, int k){
List<List<Point>> listOfLists = new ArrayList<List<Point>>();
int [] set = new int[points.length];
Arrays.fill(set, -1);
Arrays.sort(points, (a,b)-> a.x - b.x);
HashMap<Integer, List<Point>> map = new HashMap<>();
for(int i = 0; i < points.length; i++){
for(int j = i + 1; j < points.length; j++){
if(points[j].x - points[i].x > k)break;
int distance = getDistance(points[i], points[j]);
if(distance > k)break;
int rootOfI = find(set, i);
int rootOfJ = find(set, j);
union(set,rootOfI,rootOfJ);
}
}
for(int i = 0; i < set.length; i++){
int root = find(set, i);
map.putIfAbsent(root, new ArrayList<Point>());
map.get(root).add(points[i]);
}
for(Map.Entry<Integer, List<Point>> entry: map.entrySet()){
if(entry.getValue().size() > 1)
listOfLists.add(entry.getValue());
}
return listOfLists;
}
int getDistance(Point i, Point j){
double xVal = Math.pow(Math.abs(i.x - j.x), 2);
double yVal = Math.pow(Math.abs(i.y - j.y), 2);
double distance = Math.sqrt(xVal + yVal);
return (int)distance;
}
int find(int [] set, int i){
if(set[i] < 0)return i;
return set[i] = find(set,set[i]);
}
void union(int [] set, int root1, int root2){
if(set[root1] < set[root2]){
set[root2] = root1;
set[root1]--;
}else{
if(set[root1] == set[root2])set[root2]--;
set[root1] = root2;
}
}
Ok, why can't it be done in O(NLogN) then. If we get a rectangle shape around the point, then we can just check the x value which is closest to the given point, no ?
public class Point {
public Double getX() {
return x;
}
public Double getY() {
return y;
}
private final Double x;
private final Double y;
public Point(Double x, Double y) {
this.x = x;
this.y = y;
}
}
public class Solution {
public double distance(Point a, Point b) {
final double squaredSum = Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2);
return Math.sqrt(squaredSum);
}
public Integer numberOFGroups(List<Point> points, Double k) {
final Set<Point> notGrouped = new HashSet<>(points);
final Queue<Point> toVisit = new LinkedList<>();
Integer groupCounter = 0;
for (Point p : points) {
if (notGrouped.contains(p)) {
notGrouped.remove(p);
toVisit.add(p);
while(!toVisit.isEmpty()) {
Point visiting = toVisit.poll();
for(Iterator<Point> i = notGrouped.iterator(); i.hasNext();) {
Point checking = i.next();
if (distance(visiting, checking) <= k) {
i.remove();
toVisit.add(checking);
}
}
}
groupCounter++;
}
}
return groupCounter;
}
}
As you said O(N^2) answer is pretty straight forward, for each element we check rest of the elements to see if their difference is less than K
One way to get time complexity down to O(N) or O(NlogN) by using a sliding window.
O(N) Time complexity if the input is sorted, else we sort the input and that gives O(NlogN) complexity.
The sliding window will work like below
input: {1,2,5,10} k = 5
We start with the first 2 elements.
i = 0, j = 1
If the difference between the first and last element (j - i) of the slide is <= k, we increase the sliding window to right (j++)
When the difference becomes > k, we increment i (i++)
Every time a new digit gets added to the window, it adds (j-i) number of additional groups to the total.
Example:
input: {1,2,5,10} K = 5
sliding window : [1,2] . total Groups: 1 [[1,2]]
since 2 - 1 <= 5 ==> j++;
sliding window : [1,2, 5] . total Groups: 3 [[1,2], [2,5],[1,5]]
since 5 - 1 <= 5 ==> j++;
sliding window : [1,2, 5, 10] . 10-1 > k so we increment i
sliding window : [2, 5, 10] . 10-2 > k so we increment i
sliding window : [5, 10] . total Groups: 3 [[1,2], [2,5],[1,5], [5,10]]
Here is approach I could think of. Not fully tested
- sort input on the basis of x coordinate
- use divide and conquer .. as in merge sort
- for each merge step .. <this part is tricky> think how we can merge / form groups in O(n)
... One way could be from left and right array use last and first element and move in left or right away depending on the result of first comparison .. e.g. if xl - xr < K but dist(pl, pr) > K then use y to move in arrays.
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.
- divm01986 December 29, 2019