Google Interview Question for Software Engineer Interns


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.LinkedList;
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<>();

        points.add(new Point(2,1));
        points.add(new Point(2,2));
        points.add(new Point(1,2));
        points.add(new Point(0,2));
        points.add(new Point(0,1));
        points.add(new Point(0,0));
        points.add(new Point(1,0));
        points.add(new Point(2,0));

        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++;
            }
        }
    }

}

- Paras November 14, 2016 | Flag Reply
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

- yuval November 15, 2016 | 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