Skip to main content

Classifying Triangles by Lengths - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min readAsked at: Aon
Practice this problem

Problem Statement

Table: Triangles

+-------------+------+ 
| Column Name | Type | 
+-------------+------+ 
| A           | int  | 
| B           | int  |
| C           | int  |
+-------------+------+
(A, B, C) is the primary key for this table.
Each row include the lengths of each of a triangle's three sides.

Write a query to find the type of triangle. Output one of the following for each row:

  • Equilateral: It's a triangle with 3 sides of equal length.
  • Isosceles: It's a triangle with 2 sides of equal length.
  • Scalene: It's a triangle with 3 sides of differing lengths.
  • Not A Triangle: The given values of A, B, and C don't form a triangle.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Triangles table:
+----+----+----+
| A  | B  | C  |
+----+----+----+
| 20 | 20 | 23 |
| 20 | 20 | 20 |
| 20 | 21 | 22 |
| 13 | 14 | 30 |
+----+----+----+
Output: 
+----------------+
| triangle_type  | 
+----------------+
| Isosceles      | 
| Equilateral    |
| Scalene        |
| Not A Triangle |
+----------------+
Explanation: 
- Values in the first row from an Isosceles triangle, because A = B.
- Values in the second row from an Equilateral triangle, because A = B = C.
- Values in the third row from an Scalene triangle, because A != B != C.
- Values in the fourth row cannot form a triangle, because the combined value of sides A and B is not larger than that of side C.

Approach Overview

Problem Overview: Each row in the table contains three side lengths of a triangle. Your task is to classify the triangle as Equilateral, Isosceles, Scalene, or Not A Triangle based on the triangle inequality rule and equality of sides.

The key rule is the triangle inequality: the sum of any two sides must be greater than the third side. If this condition fails for any pair, the sides cannot form a triangle. Once the triangle validity is confirmed, classification becomes a straightforward comparison of the three side lengths.

Approach 1: Using CASE WHEN Statement (O(n) time, O(1) space)

This problem maps directly to conditional logic in SQL. You iterate through each row of the table and evaluate the triangle conditions using a CASE WHEN expression. The first check ensures the triangle inequality holds: a + b > c, a + c > b, and b + c > a. If any of these fail, return 'Not A Triangle'.

If the row passes the validity check, classify the triangle using side equality comparisons. When all three sides are equal (a = b AND b = c), the triangle is Equilateral. If exactly two sides match (a = b OR a = c OR b = c), it is Isosceles. If all sides are different, the triangle is Scalene. The CASE expression evaluates conditions sequentially, so ordering matters: check validity first, then equilateral, then isosceles.

This approach works efficiently because SQL evaluates each row independently. The database engine simply applies conditional checks without joins or additional scans, resulting in linear time relative to the number of rows in the table.

From a SQL perspective, this is a classic conditional transformation problem. It demonstrates how to embed business logic directly in queries using CASE expressions. These patterns appear frequently in database reporting queries and analytics pipelines where raw values must be categorized.

Recommended for interviews: The CASE WHEN approach is the expected solution. Interviewers want to see that you know the triangle inequality rule and can translate conditional logic into SQL. There is no need for subqueries or joins. A clean CASE expression with properly ordered conditions shows strong SQL fundamentals.

Solution

We can use the CASE WHEN statement to determine the type of the triangle.

First, we need to determine whether the three sides can form a triangle. If not, we return Not A Triangle.

Then, we check if the lengths of the three sides are equal. If they are, we return Equilateral.

Next, we check if there are two sides with equal length. If there are, we return Isosceles.

Otherwise, it means that the lengths of the three sides are all different, so we return Scalene.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
CASE WHEN Conditional ClassificationO(n)O(1)Best general solution for SQL queries where each row must be categorized based on conditions.
CASE with Precomputed Validity CheckO(n)O(1)Useful when separating triangle validation logic from classification for readability in larger SQL pipelines.

Video Solution

Leetcode 3053 - Classifying Triangles by Lengths - Solved & Explained by Everyday Data Science • Everyday Data Science • 570 views views

Frequently Asked Questions

Is Classifying Triangles by Lengths easy or hard?
Classifying Triangles by Lengths is considered an Easy problem. The challenge mainly involves remembering the triangle inequality rule and expressing conditional logic correctly using SQL CASE statements.
Classifying Triangles by Lengths Python/Java solution
Most platforms present this problem as a SQL query rather than a Python or Java algorithm. The logic would be similar in any language: validate triangle inequality and then compare the three sides to determine if the triangle is equilateral, isosceles, or scalene.
How to solve Classifying Triangles by Lengths in O(n)?
Use a CASE WHEN expression that checks triangle inequality conditions and side equality directly within the SELECT query. Since SQL processes each row independently, the database performs a constant number of comparisons per row, resulting in O(n) total time complexity.
What is the best approach for Classifying Triangles by Lengths?
The best approach uses a SQL CASE WHEN statement to evaluate triangle validity and side equality. First check the triangle inequality rule (a + b > c, a + c > b, b + c > a). If valid, classify based on equality of sides as Equilateral, Isosceles, or Scalene. This runs in O(n) time since each row is evaluated once.
Is Classifying Triangles by Lengths asked at Google/Amazon/Meta?
Triangle classification itself is more common in SQL screening rounds or database-focused interviews rather than advanced algorithm interviews. Similar conditional classification problems appear in coding interviews at companies that evaluate SQL proficiency for data roles.
What data structure is used in Classifying Triangles by Lengths?
No additional data structure is required. The solution relies on SQL conditional logic using the CASE WHEN statement and simple arithmetic comparisons between the three side length columns.
What is the time complexity of Classifying Triangles by Lengths?
The query runs in O(n) time where n is the number of rows in the table. Each row requires a constant number of comparisons inside a CASE expression. Space complexity is O(1) because no additional storage or intermediate structures are required.

Ready to solve this problem?

Practice Classifying Triangles by Lengths with our built-in code editor and test cases.

Practice on FleetCode