## Kaseya Interview Question for Java Developers

• 0

Country: India
Interview Type: In-Person

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

The solution bellow assumes only integers will be used for triangle side length.

An equiliteral triangle has all 3 sides equal
Example 3 3 3

An isosceles trianglle has 2 sides equal
Example 3 2 3

A rightangle triangle has one side squared equal to the sum of the other two sides square (Pythagoras theorem)
Example 3 4 5 (3*3 + 4*4 = 5*5)

``````static void solution(String fname) {
if (fname == null) {
return;
}
String line = null;
try {
while (line != null) {
int[] sides = parseTriangleData(line);
displayResult(sides);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
try {
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

static int[] parseTriangleData(String line) {
if (line == null) {
return null;
}
String[] triangleSides = line.split("\\s+");
if (triangleSides.length != 3) {
System.err
.println("Triangle sides data is corrupt (not equal to 3)");
return null;
}
int[] sides = new int;
for (int i = 0; i < sides.length; i++) {
sides[i] = Integer.parseInt(triangleSides[i]);
}
return sides;
}

static void displayResult(int[] sides) {
if (sides == null) {
return;
}
StringBuilder result = new StringBuilder();
if (sides == sides && sides == sides) {
result.append("equilateral");
} else if (sides == sides || sides == sides
|| sides == sides) {
result.append("isosceles");
}

if (isRightTriangle(sides)) {
if(result.length() > 0) {
result.append(" and ");
}
result.append("right-angled");
}

if(result.length() == 0) {
result.append("None");
}

System.out.println("Triangle is " + result.toString());
}

static boolean isRightTriangle(int[] sides) {

int a_squared = (int) Math.pow(sides, 2);
int b_squared = (int) Math.pow(sides, 2);
int c_squared = (int) Math.pow(sides, 2);

if ((a_squared + b_squared == c_squared)
|| a_squared + c_squared == b_squared
|| b_squared + c_squared == a_squared) {
return true;
}

return false;
}

public static void main(String[] args) {
solution("triangle.txt");
}``````

Comment hidden because of low score. Click to expand.
0

nem, the isRightTriangle does not work for large values.Example:
a,b,c 30000 40000 50000
squares 900000000 1600000000 2147483647
the squares overflow int values silently.
Something like
long a_squared = (long) sides * sides; works for larger values. You could also use 'scalene' for 'None' in the output.

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.

### 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.