Google Interview Question for SDE-2s


Country: United States




Comment hidden because of low score. Click to expand.
4
of 4 vote

observation:
after you calculate a circle - find all points on the circle (o(n))
since a triplet form only one circle - so all combination of point on that circle form the same circle.
so there is no need to re-calculate a circle with them.
storing them in a hash and skipping them in the future will reduce run-time.
you will still go over all triplets (o(n^3)) but you will call the method getCircle(...) much fewer times.

- plarion October 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Also, we can find out what point groups can not mutually create a circle. How? Simple, any 3 points on a plane can be made into a circle, unless all 3 of them form a line.

It is O(n^2) to check all points to find out all lines, at most.
So, we would have point groups which can not be used as circle.
That automatically should reduce a lot of points comparison.

They, might be possibly more optimisations, but am thinking still. Hence a comment, not an answer.

- NoOne October 30, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

With a few draws, I come to an observation - but unable to prove it yet, that is, among the list, find 4 points with the highest x,y and lowest x,y, it seems the circle must include at least 2 points among those 4. (Again, just observation, not a proved fact). If so, we can always pick the 2 among the list and find another point using the trivial method you mentioned in O(n) time.

- tdinh January 29, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't this increase the asymptotic complexity to O(n^4)? You're still calling getCircle() O(n^3) times, and for each call, you look for other points on that circle, which is O(n)... not sure this is an optimization at all.

- Anonymous August 16, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am tempted to say it is not possible to avoid O(n^3) in general. Consider a case where you have N points but only 3 of them lie on a circle (i.e. getCircle returns non-null). Now you cannot find the circle unless you test all triplets.

- Anonymous October 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look into Welzl's algorithm it's a O(log n) implementation for finding the center of a circle using Randomization.

- Anonymous October 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What would be the recursive algorithm for this?

- Ubiquetous November 02, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think getCircle already returns center point and radius right. Say it returns center point as (p,q), and radius = r

After finding the triplet, for each other point in the list
square-root of ((x-p)^2 + (y-q)^2) == radius will give the points in that circle perimeter.
Buffer all these points in a list.

Repeat above for other triplet combinations, but skip the combination where all three points are already part of a circle identified.

- Chaitanya Srikakolapu November 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't this be O(N4). O(N3) for each combination and iterating over all the point once again to calculate the distance.

- Confused Aatma November 27, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can think an optimization where we create a fully connected graph, where the edge weight represent distance between 2 points sqrt((a2-a1)2 + (b2-b1)2). Store these edges in a sorted order list. This would take O(n2). Now when we choose triplets and get a circle, we start iterating over nearest neighbor from each of 3 points, till a stage where the distance is less than diameter. This way when we ignore all the points which will definitely be out of circle.

- Confused Aatma November 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Observation:
After a few sketches, I have an yet-to-prove observation that the circle if exits must include 2 points among the following 4 points: lowest_x, higest_x, lowest_y and highest_y. If that's the case, clearly we can have a O(n) solution to this problem. Or at least, I have a feeling that solving this problem may rely on some geometry observation from which the problem can be greatly simplified...

- tdinh January 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think that will give you the largest circle. The circle with the most points on its perimeter chosen from among the points in the set might have a smaller radius. I could define a circle with radius 2 centered at (0,0), and pick 100 points on its perimeter, then add three more points on a circle of radius 10 that is also centered at (0,0).

- Esteban Cero April 23, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does anybody have any other ideas ? Nobody seems to have cracked this question.

- Anonymous February 25, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea is if all three points have to lie on circle's circumference than distance of all 3 points from centre should be same. Now we have to find such centre.

find min x and max x
find min y and max y

for (i = min_x; i <= max_x; i++)
{
for (j = min_y; j <= max_y; j++)
{
a = (x1-i)^2 + (y1-j)^2;
b = (x2-i)^2 + (y2-j)^2;
c = (x3-i)^2 + (y3-j)^2;

if (a == b && a == c)
// this is the centre
return;
}
}

- Shipra January 17, 2021 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More