Google Interview Question for SDE-2s


Country: United States




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

To see if it is an impossible triangle (or error): we need to check, for all sides, if one side of a triangle is less than the sum of the other two sides. If the check fails for any side, return error.

Otherwise, put all sides into a hashmap and check the size.
If size = 3, return scalene
If size = 2 return isoceles
If size = 1 return equilateral

Also check for edge cases and things like negative integer values, non-integer inputs etc...

- Anonymous December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

I think there is a way to do this with exclusive or but here is better but obviously not the best way:

public static int findTriangleType(int[] sideLengths) {
		if(sideLengths.length != 3) {
			return 4;
		}
		boolean pair1 = sideLengths[0] == sideLengths[1];
		boolean pair2 = sideLengths[1] == sideLengths[2];
		boolean pair3 = sideLengths[2] == sideLengths[0];
		if(pair1 && pair2 && pair3) {
			return 3;
		}
		else if (!pair1 && !pair2 && !pair3) {
			return 1;
		}
		return 2;
	}

- Anonymous December 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it worked perfectly for the general cases. how about other test cases ?

- samotgun December 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Needs a check if sum of any two is smaller than 3rd and for negativity

- DS January 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Required test cases may include (100, 1, 2), (-2, -3, -4) and your solution will return 1 although they can't be triangle.

The first line of your code should be

if(sideLengths.length == 3 && 
sideLengths[0] > 0 && sideLengths[1] > 0 && sideLengths[2] > 0 &&
sideLengths[0] + sideLengths[1] > sideLengths[2] && sideLengths[1] + sideLengths[2] > sideLengths[0] && sideLengths[2] + sideLengths[0] > sideLengths[1]) { ...

and as you can see above, making variables have shorter name or assign them to variables (int a = sideLengths[0] etc.) may help.

- yuichiro1990 April 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Suppose the integers are a, b and c.
(1)int arr[3]; arr[0] = a; arr[1] = b; arr[2] = c;
(2)if(arr[0] > arr[1]) swap(arr, 0, 1);
if(arr[1] > arr[2]) swap(arr, 1, 2);
if(arr[0] > arr[1]) swap(arr, 0, 1);
(3)if(arr[0] <= 0 || arr[2] >= arr[0]+arr[1]) return error;
if(arr[0] == arr[1]){
if(arr[1] == arr[2]) return equilateral;
else return isosceles;
}
else if(arr[1] == arr[2]) return isosceles;
else return scalene;

- uuuouou December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about following these steps
1.

if(a <= 0 || b <= 0 || c <= 0)
        return error;
    if(a == b && a == c)
        return equilateral;
    else if (a==b || b==c || a==c)
	return isosceles;
    else
    {
	if( //sort these three numbers and if smallest+middle > largest)
		return scalene;
	else 
		return error;
	}

- Mohit March 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

triangle type detection code:

#include <algorithm>

int triangleType(int a, int b, int c)
{
    enum ERet{
        scalene = 1,
        isosceles = 2,
        equilateral = 3,
        error = 4
    };
    if(a <= 0 || b <= 0 || c <= 0)
        return error;
    if(a == b && a == c)
        return equilateral;
    //sort a >= b >= c
    if(a < b)
        std::swap(a, b);
    if(a < c)
        std::swap(a, c);
    if(b < c) 
        std::swap(b, c);
    if(a - b >= c)
        return error;
    if(a == b || b == c)
        return isosceles;
    return scalene;
}

The test code is

#include <iostream>

int main()
{
    //genTests();
    const int kTestCase[][4] = {    //generated by genTests()
        {-1, -1, -1, 4},
        {-1, -1, 0, 4},
        {-1, -1, 1, 4},
        {-1, -1, 2, 4},
        {-1, -1, 3, 4},
        {-1, -1, 4, 4},
        {-1, 0, -1, 4},
        {-1, 0, 0, 4},
        {-1, 0, 1, 4},
        {-1, 0, 2, 4},
        {-1, 0, 3, 4},
        {-1, 0, 4, 4},
        {-1, 1, -1, 4},
        {-1, 1, 0, 4},
        {-1, 1, 1, 4},
        {-1, 1, 2, 4},
        {-1, 1, 3, 4},
        {-1, 1, 4, 4},
        {-1, 2, -1, 4},
        {-1, 2, 0, 4},
        {-1, 2, 1, 4},
        {-1, 2, 2, 4},
        {-1, 2, 3, 4},
        {-1, 2, 4, 4},
        {-1, 3, -1, 4},
        {-1, 3, 0, 4},
        {-1, 3, 1, 4},
        {-1, 3, 2, 4},
        {-1, 3, 3, 4},
        {-1, 3, 4, 4},
        {-1, 4, -1, 4},
        {-1, 4, 0, 4},
        {-1, 4, 1, 4},
        {-1, 4, 2, 4},
        {-1, 4, 3, 4},
        {-1, 4, 4, 4},
        {0, -1, -1, 4},
        {0, -1, 0, 4},
        {0, -1, 1, 4},
        {0, -1, 2, 4},
        {0, -1, 3, 4},
        {0, -1, 4, 4},
        {0, 0, -1, 4},
        {0, 0, 0, 4},
        {0, 0, 1, 4},
        {0, 0, 2, 4},
        {0, 0, 3, 4},
        {0, 0, 4, 4},
        {0, 1, -1, 4},
        {0, 1, 0, 4},
        {0, 1, 1, 4},
        {0, 1, 2, 4},
        {0, 1, 3, 4},
        {0, 1, 4, 4},
        {0, 2, -1, 4},
        {0, 2, 0, 4},
        {0, 2, 1, 4},
        {0, 2, 2, 4},
        {0, 2, 3, 4},
        {0, 2, 4, 4},
        {0, 3, -1, 4},
        {0, 3, 0, 4},
        {0, 3, 1, 4},
        {0, 3, 2, 4},
        {0, 3, 3, 4},
        {0, 3, 4, 4},
        {0, 4, -1, 4},
        {0, 4, 0, 4},
        {0, 4, 1, 4},
        {0, 4, 2, 4},
        {0, 4, 3, 4},
        {0, 4, 4, 4},
        {1, -1, -1, 4},
        {1, -1, 0, 4},
        {1, -1, 1, 4},
        {1, -1, 2, 4},
        {1, -1, 3, 4},
        {1, -1, 4, 4},
        {1, 0, -1, 4},
        {1, 0, 0, 4},
        {1, 0, 1, 4},
        {1, 0, 2, 4},
        {1, 0, 3, 4},
        {1, 0, 4, 4},
        {1, 1, -1, 4},
        {1, 1, 0, 4},
        {1, 1, 1, 3},
        {1, 1, 2, 4},
        {1, 1, 3, 4},
        {1, 1, 4, 4},
        {1, 2, -1, 4},
        {1, 2, 0, 4},
        {1, 2, 1, 4},
        {1, 2, 2, 2},
        {1, 2, 3, 4},
        {1, 2, 4, 4},
        {1, 3, -1, 4},
        {1, 3, 0, 4},
        {1, 3, 1, 4},
        {1, 3, 2, 4},
        {1, 3, 3, 2},
        {1, 3, 4, 4},
        {1, 4, -1, 4},
        {1, 4, 0, 4},
        {1, 4, 1, 4},
        {1, 4, 2, 4},
        {1, 4, 3, 4},
        {1, 4, 4, 2},
        {2, -1, -1, 4},
        {2, -1, 0, 4},
        {2, -1, 1, 4},
        {2, -1, 2, 4},
        {2, -1, 3, 4},
        {2, -1, 4, 4},
        {2, 0, -1, 4},
        {2, 0, 0, 4},
        {2, 0, 1, 4},
        {2, 0, 2, 4},
        {2, 0, 3, 4},
        {2, 0, 4, 4},
        {2, 1, -1, 4},
        {2, 1, 0, 4},
        {2, 1, 1, 4},
        {2, 1, 2, 2},
        {2, 1, 3, 4},
        {2, 1, 4, 4},
        {2, 2, -1, 4},
        {2, 2, 0, 4},
        {2, 2, 1, 2},
        {2, 2, 2, 3},
        {2, 2, 3, 2},
        {2, 2, 4, 4},
        {2, 3, -1, 4},
        {2, 3, 0, 4},
        {2, 3, 1, 4},
        {2, 3, 2, 2},
        {2, 3, 3, 2},
        {2, 3, 4, 1},
        {2, 4, -1, 4},
        {2, 4, 0, 4},
        {2, 4, 1, 4},
        {2, 4, 2, 4},
        {2, 4, 3, 1},
        {2, 4, 4, 2},
        {3, -1, -1, 4},
        {3, -1, 0, 4},
        {3, -1, 1, 4},
        {3, -1, 2, 4},
        {3, -1, 3, 4},
        {3, -1, 4, 4},
        {3, 0, -1, 4},
        {3, 0, 0, 4},
        {3, 0, 1, 4},
        {3, 0, 2, 4},
        {3, 0, 3, 4},
        {3, 0, 4, 4},
        {3, 1, -1, 4},
        {3, 1, 0, 4},
        {3, 1, 1, 4},
        {3, 1, 2, 4},
        {3, 1, 3, 2},
        {3, 1, 4, 4},
        {3, 2, -1, 4},
        {3, 2, 0, 4},
        {3, 2, 1, 4},
        {3, 2, 2, 2},
        {3, 2, 3, 2},
        {3, 2, 4, 1},
        {3, 3, -1, 4},
        {3, 3, 0, 4},
        {3, 3, 1, 2},
        {3, 3, 2, 2},
        {3, 3, 3, 3},
        {3, 3, 4, 2},
        {3, 4, -1, 4},
        {3, 4, 0, 4},
        {3, 4, 1, 4},
        {3, 4, 2, 1},
        {3, 4, 3, 2},
        {3, 4, 4, 2},
        {4, -1, -1, 4},
        {4, -1, 0, 4},
        {4, -1, 1, 4},
        {4, -1, 2, 4},
        {4, -1, 3, 4},
        {4, -1, 4, 4},
        {4, 0, -1, 4},
        {4, 0, 0, 4},
        {4, 0, 1, 4},
        {4, 0, 2, 4},
        {4, 0, 3, 4},
        {4, 0, 4, 4},
        {4, 1, -1, 4},
        {4, 1, 0, 4},
        {4, 1, 1, 4},
        {4, 1, 2, 4},
        {4, 1, 3, 4},
        {4, 1, 4, 2},
        {4, 2, -1, 4},
        {4, 2, 0, 4},
        {4, 2, 1, 4},
        {4, 2, 2, 4},
        {4, 2, 3, 1},
        {4, 2, 4, 2},
        {4, 3, -1, 4},
        {4, 3, 0, 4},
        {4, 3, 1, 4},
        {4, 3, 2, 1},
        {4, 3, 3, 2},
        {4, 3, 4, 2},
        {4, 4, -1, 4},
        {4, 4, 0, 4},
        {4, 4, 1, 2},
        {4, 4, 2, 2},
        {4, 4, 3, 2},
        {4, 4, 4, 3},
        {0x7ffffffd, 0x7ffffffd, 0x7ffffffd, 3},
        {0x7ffffffd, 0x7ffffffd, 0x7ffffffe, 2},
        {0x7ffffffd, 0x7ffffffd, 0x7fffffff, 2},
        {0x7ffffffd, 0x7ffffffd, 0x80000000, 4},
        {0x7ffffffd, 0x7ffffffe, 0x7ffffffd, 2},
        {0x7ffffffd, 0x7ffffffe, 0x7ffffffe, 2},
        {0x7ffffffd, 0x7ffffffe, 0x7fffffff, 1},
        {0x7ffffffd, 0x7ffffffe, 0x80000000, 4},
        {0x7ffffffd, 0x7fffffff, 0x7ffffffd, 2},
        {0x7ffffffd, 0x7fffffff, 0x7ffffffe, 1},
        {0x7ffffffd, 0x7fffffff, 0x7fffffff, 2},
        {0x7ffffffd, 0x7fffffff, 0x80000000, 4},
        {0x7ffffffd, 0x80000000, 0x7ffffffd, 4},
        {0x7ffffffd, 0x80000000, 0x7ffffffe, 4},
        {0x7ffffffd, 0x80000000, 0x7fffffff, 4},
        {0x7ffffffd, 0x80000000, 0x80000000, 4},
        {0x7ffffffe, 0x7ffffffd, 0x7ffffffd, 2},
        {0x7ffffffe, 0x7ffffffd, 0x7ffffffe, 2},
        {0x7ffffffe, 0x7ffffffd, 0x7fffffff, 1},
        {0x7ffffffe, 0x7ffffffd, 0x80000000, 4},
        {0x7ffffffe, 0x7ffffffe, 0x7ffffffd, 2},
        {0x7ffffffe, 0x7ffffffe, 0x7ffffffe, 3},
        {0x7ffffffe, 0x7ffffffe, 0x7fffffff, 2},
        {0x7ffffffe, 0x7ffffffe, 0x80000000, 4},
        {0x7ffffffe, 0x7fffffff, 0x7ffffffd, 1},
        {0x7ffffffe, 0x7fffffff, 0x7ffffffe, 2},
        {0x7ffffffe, 0x7fffffff, 0x7fffffff, 2},
        {0x7ffffffe, 0x7fffffff, 0x80000000, 4},
        {0x7ffffffe, 0x80000000, 0x7ffffffd, 4},
        {0x7ffffffe, 0x80000000, 0x7ffffffe, 4},
        {0x7ffffffe, 0x80000000, 0x7fffffff, 4},
        {0x7ffffffe, 0x80000000, 0x80000000, 4},
        {0x7fffffff, 0x7ffffffd, 0x7ffffffd, 2},
        {0x7fffffff, 0x7ffffffd, 0x7ffffffe, 1},
        {0x7fffffff, 0x7ffffffd, 0x7fffffff, 2},
        {0x7fffffff, 0x7ffffffd, 0x80000000, 4},
        {0x7fffffff, 0x7ffffffe, 0x7ffffffd, 1},
        {0x7fffffff, 0x7ffffffe, 0x7ffffffe, 2},
        {0x7fffffff, 0x7ffffffe, 0x7fffffff, 2},
        {0x7fffffff, 0x7ffffffe, 0x80000000, 4},
        {0x7fffffff, 0x7fffffff, 0x7ffffffd, 2},
        {0x7fffffff, 0x7fffffff, 0x7ffffffe, 2},
        {0x7fffffff, 0x7fffffff, 0x7fffffff, 3},
        {0x7fffffff, 0x7fffffff, 0x80000000, 4},
        {0x7fffffff, 0x80000000, 0x7ffffffd, 4},
        {0x7fffffff, 0x80000000, 0x7ffffffe, 4},
        {0x7fffffff, 0x80000000, 0x7fffffff, 4},
        {0x7fffffff, 0x80000000, 0x80000000, 4},
        {0x80000000, 0x7ffffffd, 0x7ffffffd, 4},
        {0x80000000, 0x7ffffffd, 0x7ffffffe, 4},
        {0x80000000, 0x7ffffffd, 0x7fffffff, 4},
        {0x80000000, 0x7ffffffd, 0x80000000, 4},
        {0x80000000, 0x7ffffffe, 0x7ffffffd, 4},
        {0x80000000, 0x7ffffffe, 0x7ffffffe, 4},
        {0x80000000, 0x7ffffffe, 0x7fffffff, 4},
        {0x80000000, 0x7ffffffe, 0x80000000, 4},
        {0x80000000, 0x7fffffff, 0x7ffffffd, 4},
        {0x80000000, 0x7fffffff, 0x7ffffffe, 4},
        {0x80000000, 0x7fffffff, 0x7fffffff, 4},
        {0x80000000, 0x7fffffff, 0x80000000, 4},
        {0x80000000, 0x80000000, 0x7ffffffd, 4},
        {0x80000000, 0x80000000, 0x7ffffffe, 4},
        {0x80000000, 0x80000000, 0x7fffffff, 4},
        {0x80000000, 0x80000000, 0x80000000, 4}
    };
    for(int i = 0;i < sizeof kTestCase / sizeof kTestCase[0];++i)
        if(kTestCase[i][3] != triangleType(kTestCase[i][0], kTestCase[i][1], kTestCase[i][2]))
            std::cerr<<"triangle ["<<kTestCase[i][0]<<", "<<kTestCase[i][1]
                <<", "<<kTestCase[i][2]<<"] is not "<<kTestCase[i][3]<<"\n";
}

All the test cases is generated by the following code

//generate test cases
void genTests()
{
    //test case
    for(int a = -1;a <= 4; ++a)
        for(int b = -1;b <= 4; ++b)
            for(int c = -1;c <= 4; ++c){
                std::cout<<"{"<<a<<", "<<b<<", "<<c<<", "
                    <<triangleType(a, b, c)<<"},\n";
            }
    const int kMax = 0x7FFFFFFF;
    for(int a = -2;a <= 1; ++a)
        for(int b = -2;b <= 1; ++b)
            for(int c = -2;c <= 1; ++c){
                std::cout<<std::hex<<"{0x"<<(a + kMax)<<", 0x"<<(b + kMax)<<", 0x"<<(c + kMax)<<", "
                    <<triangleType(a + kMax, b + kMax, c + kMax)<<"},\n";
            }
}

- DoZerg December 21, 2013 | 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