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.
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.
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 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.
Suppose you are given side lengths [4, 4, 9].
[4, 4, 9].[5, 5, 5]:[3, 4, 5]:The most frequent error is skipping the validity check entirely and jumping straight to classifying the triangle. Without checking , 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.
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.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Product of Three Numbers | Easy | Solve | |
| Minimum Moves to Equal Array Elements II | Medium | Solve | |
| Maximum Building Height | Hard | Solve | |
| Largest Perimeter Triangle | Easy | Solve | |
| Sort Array By Absolute Value | Easy | Solve |