Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

1. find angle of all points from x axis
2. sort on basis of angle
3. use sliding window to find mamimum number of elemnts that can be present in window and having angle less than 45 deg from 1st element of window

- shivamkmrjha November 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. calculate angle of all points from x axis
2. sort on basis of angle
3. use sliding window to find maximum no. of elements such each element in sliding window is having less than 45 deg from 1st elemnt of sliding window.
time complexity (O(nlogn))

- shivam kumar November 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from math import atan2, degrees
def max_viewing_angle(points, v):
    angles = [degrees(atan2(y, x)) for x,y in points]
    angles.sort()
    
    start = 0
    end = 0
    max_points = None
    while end < len(angles):
        if max_points == None:
            max_points = (end-start, angles[start])
        
        diff = angles[end] - angles[start]
        if diff <= v:
            end += 1
        else:
            start += 1
            
        points = end - start
        if points > max_points[0]:
            max_points = (points, angles[start])
        
    return max_points[1] + v/2
                

points = [(1,1), (3,1), (2,1),  (1,2), (-2,5), (-1,6), (1,3), (-1,3), (0,4)]
v = 45.0
print max_viewing_angle(points, v)

- Nitish Garg December 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find angles for all the points in 2D. For -ve angles subtract from 360 (only required for ease in code)
        System.out.println(Math.atan2(3, 3) * 180 / Math.PI); //45
        System.out.println(Math.atan2(3, -3) * 180 / Math.PI);  // 135
        System.out.println(Math.atan2(-3, -3) * 180 / Math.PI);   // -135  // 360 - 135 = 225
        System.out.println(Math.atan2(-3, 3) * 180 / Math.PI);  // -45  // 360 - 45 = 315

sort angles

int ei = lowerBound(List<Integer> angles, int si, int ei, int ev);
append values from 0 to ei to end of the angles array. // required for search in cicural sorted array. e.g 

angles = [1, 20, 45, 134, 260, 359, 361, 380] ..here 380 ==> 1, 390 ==>  20 
total points seen in 45 viewing angle from 359 is 3 (359, 361, 380)

int maxpoint(int angles[], ) {
	
	int max = 0;
	for(int si = 0; si < angles.length && angles[si] < 361; si ++) {
		int ev = angles[si] + 45;
		int ei = lowerBound(angles, si, angles.length, ev);
		max = Math.max(max, ei - si);
	}
	return max;
}

int lowerbound(List<Inetger> angles, int si, int ei, int ev) {
	
	if(si >= ei) return si;

	while(si < ei) {
		int mi = (si + ei)/2
		int mv = angles[mi];

		if(mv = ev) return mi;
		if(mv > ev) {
			ei = mi;
		} else {
			si = mi;
		}
	}
	return si;
}

- josbimosbi December 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <map>
#include <cmath>

#define PI 3.14159265

using namespace std;

struct Point{
	double x, y;
};

int get_angle(const Point& p){
	return atan2 (p.y,p.x) * 180 / PI;
}

int best_angle(const vector<Point>& points, int v_angle){
    int res = 0;

    multimap<int, Point> mm;

    for(int i = 0 ; i < points.size(); ++i){
        mm.insert(pair<const int, Point>(get_angle(points[i]), points[i]));
        cout << get_angle(points[i]) << " " <<  points[i].x << " " << points[i].y << endl;
    }

    int current_angle = 0;

    auto iter = mm.begin(), iterE = mm.begin();
    while(iterE != mm.end() && current_angle <= v_angle){
    	++iterE;
    	if(res < current_angle)
    	    res=current_angle;
    	current_angle = iterE->first - iter->first;

    }

   	while(iterE != mm.end()){
   		if(current_angle == v_angle){
   			res = current_angle;
   			break;
   		}
   		else if(current_angle > v_angle){
   			++iter;
   		} else{
   			++iterE;
   			if(res < current_angle)
   				res = current_angle;
   		}
    	current_angle = iterE->first - iter->first;
    }

    return res;
}

void test(int case_n, const vector<Point>& vp, int v){
		cout << "test case " << case_n << " :" << endl;
		cout << "input : " << endl;
	    cout << "angle : " << v << endl;
		cout << "points : " << endl;
	    int a = best_angle(vp, v);
	    cout << "output : " << a << endl;
	    cout << "-----------------------" << endl << endl;
}

int main() {
	test(1, vector<Point>({{10,10}, {10,5}, {6,8}, {1,9}, {9,4}}), 0);
	test(2, vector<Point>({{10,10}, {10,5}, {6,8}, {1,9}, {9,4}}), 15);
	test(3, vector<Point>({{10,10}, {10,5}, {6,8}, {1,9}, {9,4}}), 45);

	test(4, vector<Point>({{10,10}, {10,5}, {6,8}, {3,9}, {9,4}}), 0);
	test(5, vector<Point>({{10,10}, {10,5}, {6,8}, {3,9}, {9,4}}), 45);
	test(6, vector<Point>({{10,10}, {10,5}, {6,8}, {3,9}, {9,4}}), 60);

	test(7, vector<Point>({{10,10}, {10,5}, {6,8}, {4,9}, {9,4}}), 31);
	test(8, vector<Point>({{10,10}, {10,5}, {6,8}, {4,9}, {9,4}}), 72);
	test(9, vector<Point>({{10,10}, {10,5}, {6,8}, {4,9}, {9,4}}), 180);

	return 0;

}

- dolphinden June 16, 2017 | 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