Google Interview Question for Software Engineer Interns

• 0

Country: United States
Interview Type: Phone Interview

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

O(nlogn) Solution.
1. consider the given view point as center of axis and calculate the angle of each point.
2. sort the angles
3. find the maximum window in the sorted list with max difference of given viewing angle.

import java.util.Collections;
import java.util.List;

/**
* Created by paras.mal on 14/11/16.
*/
public class A2 {

static class Point{

int x,  y;
public Point(int x, int y){
this.x = x;
this.y = y;
}

}
public static double getAngle(int x, int y, int x1, int y1){

x = x1-x;
y = y1-y;

if(x > 0 && y >= 0){
return Math.toDegrees(Math.asin(Math.abs(y) / Math.sqrt(x * x + y * y)));
}

if(x<=0 && y>0){
return Math.toDegrees(Math.asin(Math.abs(x)/Math.sqrt(x*x + y*y))) + 90;
}

if( x < 0 && y<=0){
return Math.toDegrees(Math.asin(Math.abs(y)/Math.sqrt(x*x + y*y))) + 180;
}
if( x >= 0 && y<0){
return Math.toDegrees(Math.asin(Math.abs(x)/Math.sqrt(x*x + y*y))) + 270;
}

throw new RuntimeException();

}

public static void main(String s[]){
List<Point> points = new LinkedList<>();

List<Double> d= new LinkedList<>();
int x1 = 1,y1 = 1;
points.stream().forEach((x) -> d.add(getAngle(x1, y1, x.x, x.y)));
Collections.sort(d);
int i = 0, j = 0;
int diff = 0;
int check = 90;
int i1 =0, j1 =0;
while(j<d.size() && i<d.size()){
while(j<d.size() && d.get(j)-d.get(i) <= check){
if(j1-i1 < j-i && d.get(j)-d.get(i) <= check){
j1 = j;
i1 = i;
}
j++;
}
while( i <= j && j < d.size() && d.get(j)-d.get(i) > check){
i++;
}
}
}

}

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

1. make the points angle based = angleArray
2. sort angleArray nlogn
3. for i in range(0,len(angleArray):
count how many enter in you window. = tempSum = max
4. move to next point where the is tempSum = tempSum-1 + count untill you exeed the window (you continue from the index you stopped you dont go back) so you just do here O(n)
5. if tempSum > max then max = tempSum

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.

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.