Google Interview Question
Software Engineer in TestsCountry: United States
Interview Type: Written Test
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;
}
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....
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());
}
}
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;
}
}
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
}
}
}
- Anonymous June 28, 2012