Magicsheet logo

Type of Triangle

Easy
71.7%
Updated 6/1/2025

Type of Triangle

What is this problem about?

The Type of Triangle interview question is a fundamental geometry-based coding challenge. Given three integers representing the lengths of the sides of a potential triangle, you must determine if they can form a valid triangle and, if so, what kind it is: Equilateral (all sides equal), Isosceles (two sides equal), or Scalene (all sides different). If the sides do not satisfy the triangle inequality theorem, the output should indicate that no triangle can be formed.

Why is this asked in interviews?

While seemingly simple, the Type of Triangle coding problem is a great way for interviewers at Google or Microsoft to check a candidate's attention to detail and ability to handle edge cases. It tests your knowledge of basic mathematical properties and your ability to write clean, branching logic (if-else statements). It’s less about complex algorithms and more about precision and robustness in your code.

Algorithmic pattern used

The primary Array, Math, Sorting interview pattern here involves the Triangle Inequality Theorem, which states that the sum of any two sides of a triangle must be greater than the third side (a+b>ca + b > c). A quick way to implement this is to sort the three side lengths. If the sum of the two smaller sides is greater than the largest side, a triangle is valid. Then, simple comparison logic determines the specific type based on how many sides are equal.

Example explanation

Suppose you are given side lengths [4, 4, 9].

  1. Sort the lengths: [4, 4, 9].
  2. Check validity: 4+4=84 + 4 = 8. Since 8 is not greater than 9, these sides cannot form a triangle. Now consider [5, 5, 5]:
  3. Check validity: 5+5>55 + 5 > 5 (True).
  4. Check equality: All three sides are equal, so it is an Equilateral triangle. Lastly, for [3, 4, 5]:
  5. Check validity: 3+4>53 + 4 > 5 (True).
  6. Check equality: No two sides are equal, so it is a Scalene triangle.

Common mistakes candidates make

The most frequent error is skipping the validity check entirely and jumping straight to classifying the triangle. Without checking a+b>ca+b > c, you might incorrectly label [1, 2, 10] as Isosceles. Another mistake is incorrect logical ordering—for example, checking for Isosceles before Equilateral might lead to wrong results if not handled carefully.

Interview preparation tip

In your interview preparation, always look for the simplest mathematical property that solves the problem. For geometric questions, boundary conditions are everything. Practice explaining the "Triangle Inequality" clearly, as communication is just as important as the code itself in these foundational logic tasks.

Similar Questions