Google Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Written Test




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

if(a<=0 || b<=0 || c<=0 || a+b<=c || a+c<=b || b+c<=a)
	return 4;
else if(a==b && b==c)
	return 3;
else if(a==b || b==c || a==c)
	return 2;
else
	return 1;

- Anonymous June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think interviewer is asking to test the function and not to write the function

- rawat011 June 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int triangleType(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
return 4;
}
long ab = a + b;
long bc = b + c;
long ca = c + a;
if (ab <= c || bc <= a || ca <= b) {
return 4;
}
int count = 0;
if (a == b)
count++;
if (b == c)
count++;
if (c == a)
count++;

if (count == 3)
return count;
else
return count + 1;
}

- Arjun June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

I have change the code slightly to remove the condition on return statements....

public static int triangleType(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
return 4;
}
long ab = a + b;
long bc = b + c;
long ca = c + a;
if (ab <= c || bc <= a || ca <= b) {
return 4;
}
int count = 1;
if (a == b)
count++;
if (b == c)
count++;
return count;
}

If any error please comment....

- Puneet September 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TriangleType2 {
	
	public enum Type{
		SCALENE, ISOCELES, EQUILATERAL, ERROR;	
		@Override public String toString() {
			//only capitalise the first letter
			String s = super.toString();
			return s.substring(0, 1) + s.substring(1).toLowerCase();
		}
	}
	
	static Type getTriangleType(int x, int y, int z) {
		Type type = null;
		
		if(x <= 0 || y <= 0 || z <= 0){
			type = Type.ERROR;
		} else if(x!=y && y!=z && x!=z) {
			type = Type.SCALENE;
		} else if(x==y && y==z && z==x) {
			type = Type.EQUILATERAL;
		} else if(x==y || y==z || x == z) {
			type = Type.ISOCELES;
		}		
		
		return type;
	}
	
	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("Enter the length of each side of the triangle");

		int i = 1;
		int[] side = new int[3];
		while(i<4) {
			System.out.print("Side " + i + ":\t");
			try {
				side[i-1] = Integer.parseInt(br.readLine());				
			} catch(IOException ie) {
				System.out.println("IOException caught:\t" + ie);
			}
			i++;
		}
		
		Type type = getTriangleType(side[0], side[1], side[2]);
		
		System.out.println("Triangle Type:\t" + type.toString());

	}

}

- Amruta July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class IdentifyTriangle {

    public static int Run(int a, int b, int c)
    {
        if(a>=(b+c) || b>=(a+c) || c>=(b+a) || a <= 0 || b <= 0 || c <= 0)
        {
            System.out.println("ERROR");
            return 4;
        }

        if(a==b || b==c || c==a)
        {
            System.out.println("ISOSCELES");
            return 2;
        }

        if(a==b && b==c)
        {
            System.out.println("EQUILATERAL");
            return 4;
        }

        System.out.println("SCALENE");
        return 1;
    }
}

- level42 July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int isTraingle(int a,int b,int c){
		if(a<=0 || b<=0 || c<=0)
			return 4;
		if(a+b>c && b+c>a && a+c >b){
		
			if((a==b && a!=c) || (a==c && a!=b) || (c==b && c!=a))
			return 2;
		
			else if(a==b && b==c)
			return 3;
			else
				return 1;
		
			
		}
			
	
		else
			return 4;

}

- Amit August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//Base Case for Equilateral
		System.out.println(Test.getTringleType(1,1,1));
		System.out.println(Test.getTringleType(10, 10, 10));
		System.out.println(Test.getTringleType(100, 100, 100));
		
	
		//Base Case for  Isosceles
		
		System.out.println(Test.getTringleType(2,2,3));
		System.out.println(Test.getTringleType(50,50,1));
		System.out.println(Test.getTringleType(10,2,10));
		
		
		// Base Case for Scalen
		
		System.out.println(Test.getTringleType(5,6,7));
		System.out.println(Test.getTringleType(10, 20, 15));
		System.out.println(Test.getTringleType(511, 522, 533));
		
		// Expected Errors
		
		System.out.println(Test.getTringleType(0,1,2));
		System.out.println(Test.getTringleType(0,0,0));
		System.out.println(Test.getTringleType(Integer.MAX_VALUE,1,1));
		System.out.println(Test.getTringleType(Integer.MAX_VALUE,Integer.MAX_VALUE + 1,Integer.MAX_VALUE + 2));
	}
	/**
	 * returns one of four values to determine the triangle type
	 * @author axel
	 * @param a 
	 * @param b
	 * @param c
	 * @return 1=scalene, 2=isosceles, 3=equilateral, 4=error
	 */
	public static int getTringleType(int a,int b,int c){
	
		if( a <= 0 || b <= 0 || c <= 0 || b+c<=a || a+c<=b || a+b<=c)
		{
			return 4; // Error
		}else if(a == b  && b == c ){
			return 3; // Equilateral
		}else if(a == b || b == c || a == c){
			return 2;  // Isosceles
		}else{
			
			return 1; // Scalen
		}
		
	}

}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 16, 2014 | 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