Skip to main content

Classifying Triangles by Lengths - Video Solutions

EasyDatabase

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

1 video solution available

Classifying Triangles by Lengths - Video Solution

Watch the video solution for Classifying Triangles by Lengths, a easy level problem involving Database. This walkthrough by Everyday Data Science has 570 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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.
Read full problem with examples

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.

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.