Kaseya Interview Question for Java Developers


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;
		}
		BufferedReader reader = null;
		FileReader fileReader = null;
		String line = null;
		try {
			fileReader = new FileReader(fname);
			reader = new BufferedReader(fileReader);
			line = reader.readLine();
			while (line != null) {
				int[] sides = parseTriangleData(line);
				displayResult(sides);
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} 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[3];
		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[0] == sides[1] && sides[1] == sides[2]) {
			result.append("equilateral");
		} else if (sides[0] == sides[1] || sides[1] == sides[2]
				|| sides[0] == sides[2]) {
			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[0], 2);
		int b_squared = (int) Math.pow(sides[1], 2);
		int c_squared = (int) Math.pow(sides[2], 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");
	}

- nem September 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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[0] * sides[0]; works for larger values. You could also use 'scalene' for 'None' in the output.

- dave8128 September 15, 2014 | Flag


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