Google Onsite Inteview chances?

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2025-06-10Added By Anonymous
Source: Link

Problem

Consider a bounded box, we need to place circle's with double type center point (x,y) eg (1.4, 2.3), (4.25,53.3) etc.

Write definition of below function, which places the coin if not overlapping with already placed ones.

public boolean place(Double x, Double y)

inputs: circles center point (x, y)

output: boolean whether we can place the coin without overlapping with the existing coins.


My Solution

Within given 40mins of time, I was only able to come up with a x-axis sorted solution and binsearch on x-axis invoke coins falling within x-2, x+2 range linearly.

1class CoinPlacement { 2 TreeSet<Pair<Double, Double>> sortedCoinsSet; 3 4 public CoinPlacement() { 5 this.sortedCoinsSet = new TreeSet<>(); 6 } 7 8 //N coins placed: 9 //Q queries 10 //overal complexity: O(Q * (log N + N)) 11 public boolean canPlace(Double x, Double y) { 12 Set<Pair<Double, Double>> subset = sortedCoinsSet.subSet(new Pair(x-2, y), new Pair(x+2, y)); //O(log N) 13 14 if (!subSet.isEmpty()) { 15 for (Pair<Double, Double> coins: subset) { //O(N) 16 if (overlap(coin, new Pair(x, y)) { // O(1) 17 return false; 18 } 19 } 20 } 21 sortedCoinsSet.add(new Pair(x, y)); //O(log N) 22 return true; 23 } 24}


Assume there is a overlap utility available something like

1public boolean overlap(Pair<Doube, Double> coin1, 2 Pair<Doube, Double> coin2) { 3 double dx = coin1.getKey() - coin2.getKey(); 4 double dy = coin1.getValue() - coin2.getValue(); 5 double distanceSquared = dx * dx + dy * dy; 6 double minDistanceForOverlap = 2.0; // Diameter of a coin with radius 1 7 return distanceSquared < (minDistanceForOverlap * minDistanceForOverlap); 8}


Can anyone help me understand if there can be better approach than this using advanced DS to sort on both x and y axis can be done in logX * logY time?


Also please suggest, if this solution results in Hire/No Hire?

Comments