Watch 10 video solutions for Triangle Judgement, a easy level problem involving Database. This walkthrough by Learn With Chirag has 6,457 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Triangle
+-------------+------+ | Column Name | Type | +-------------+------+ | x | int | | y | int | | z | int | +-------------+------+ In SQL, (x, y, z) is the primary key column for this table. Each row of this table contains the lengths of three line segments.
Report for every three line segments whether they can form a triangle.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Triangle table: +----+----+----+ | x | y | z | +----+----+----+ | 13 | 15 | 30 | | 10 | 20 | 15 | +----+----+----+ Output: +----+----+----+----------+ | x | y | z | triangle | +----+----+----+----------+ | 13 | 15 | 30 | No | | 10 | 20 | 15 | Yes | +----+----+----+----------+
Problem Overview: Each row contains three side lengths x, y, and z. The task is to determine whether these three values can form a valid triangle and return Yes or No for every row.
A triangle is valid only when the triangle inequality holds: the sum of any two sides must be greater than the third side. If any of these conditions fail, the sides cannot form a triangle.
Approach 1: Direct Triangle Inequality Check (O(n) time, O(1) space)
The most direct solution applies the triangle inequality rule directly inside a SQL query. For each row, check whether x + y > z, x + z > y, and y + z > x. If all three conditions hold, return Yes; otherwise return No. In SQL this is usually implemented with a CASE WHEN expression that evaluates the three comparisons and outputs the result.
The database engine simply scans each row once and performs constant-time arithmetic comparisons. No joins, sorting, or extra structures are required. This makes the solution both efficient and easy to read. The approach works well for large tables because it performs a straightforward row-by-row evaluation with O(n) time complexity and O(1) additional space.
This method is the most common solution in database interview problems because it demonstrates that you understand how to express conditional logic directly in SQL using CASE and boolean expressions.
Approach 2: Pre-sorting Approach for Inequality Checks (O(n) time, O(1) space)
This approach relies on a mathematical observation. If the three sides are sorted so that a ≤ b ≤ c, only one condition needs to be checked: a + b > c. When the two smaller sides sum to more than the largest side, the other triangle inequalities automatically hold.
In practice, you conceptually reorder the values and then test the single inequality. Some implementations compute the smallest, middle, and largest values using SQL functions such as LEAST() and GREATEST(), or equivalent logic. After ordering the values, the query checks whether the sum of the two smallest sides exceeds the largest side.
This reduces the number of logical checks and highlights the mathematical property behind the triangle rule. The runtime remains O(n) because each row still requires a constant number of operations. The technique is often discussed in problems related to math reasoning and conditional evaluation in SQL.
Recommended for interviews: The direct triangle inequality check is what most interviewers expect. It clearly shows that you know the triangle property and can translate it into SQL logic using CASE WHEN. Mentioning the sorted-side observation demonstrates deeper mathematical reasoning, but the direct check is usually preferred for clarity and simplicity.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Triangle Inequality Check | O(n) | O(1) | Best general solution. Clear SQL CASE logic and minimal computation. |
| Pre-sorting Inequality Check | O(n) | O(1) | Useful when leveraging mathematical insight that only the two smallest sides need checking. |