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.
To determine whether three segments can form a triangle, we can directly apply the triangle inequality theorem. For segments with lengths x, y, and z to form a triangle, the following conditions must be met:
x + y > zx + z > yy + z > xIf all three conditions are satisfied, then the segments can form a triangle.
This C program defines a function checkTriangle which checks if three given sides can form a triangle using the triangle inequality theorem. The main function initializes an array of sides, iterates through each set of sides, and calls checkTriangle for each set.
Time Complexity: O(1) per triangle check since we only run a constant number of comparisons.
Space Complexity: O(1) since no additional space proportional to input size is used.
In this approach, we first sort the three sides so that we only need to check one inequality condition instead of three. Given sides x, y, and z, sort them to be a ≤ b ≤ c. Then check: a + b > c.
Sorting reduces the number of comparisons and handles the integer sides efficiently when checking validity of the triangle.
Using selection-like sorting, this C program sorts the three side lengths before checking only the most significant triangle inequality condition. The sort function performs simple comparisons and swaps.
Time Complexity: O(1) since sorting three items is constant time and checking has a constant cost.
Space Complexity: O(1).
The condition for whether three sides can form a triangle is that the sum of any two sides is greater than the third side. Therefore, we can use an IF statement to determine whether this condition is satisfied. If it is satisfied, we return Yes, otherwise we return No.
MySQL
| Approach | Complexity |
|---|---|
| Approach 1: Direct Triangle Inequality Check | Time Complexity: O(1) per triangle check since we only run a constant number of comparisons. |
| Approach 2: Pre-sorting Approach for Inequality Checks | Time Complexity: O(1) since sorting three items is constant time and checking has a constant cost. |
| IF Statement + Triangle Inequality | — |
| 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. |
Triangle Judgement | Leetcode 610 | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 6,457 views views
Watch 9 more video solutions →Practice Triangle Judgement with our built-in code editor and test cases.
Practice on FleetCode