Triangle Judgement - Problem

๐Ÿ”บ Triangle Judgement

Given a table containing the lengths of three line segments for each row, your task is to determine whether these three segments can form a valid triangle.

The Challenge: In geometry, three line segments can form a triangle if and only if the sum of any two sides is greater than the third side. This is known as the Triangle Inequality Theorem.

Database Schema:

Table: Triangle
+-------------+------+
| Column Name | Type |
+-------------+------+
| x           | int  |
| y           | int  |
| z           | int  |
+-------------+------+
(x, y, z) is the primary key

Goal: Write a SQL query that returns each row with an additional column indicating whether the three segments can form a triangle. Return "Yes" if they can form a triangle, "No" otherwise.

Triangle Rule: For sides a, b, and c to form a triangle:
โ€ข a + b > c
โ€ข a + c > b
โ€ข b + c > a
All three conditions must be satisfied!

Input & Output

example_1.sql โ€” Basic Triangle
$ Input: Triangle table: +---+---+---+ | x | y | z | +---+---+---+ | 3 | 4 | 5 | +---+---+---+
โ€บ Output: +---+---+---+----------+ | x | y | z | triangle | +---+---+---+----------+ | 3 | 4 | 5 | Yes | +---+---+---+----------+
๐Ÿ’ก Note: This is a classic 3-4-5 right triangle. All conditions are satisfied: 3+4=7>5, 3+5=8>4, 4+5=9>3
example_2.sql โ€” Invalid Triangle
$ Input: Triangle table: +---+---+---+ | x | y | z | +---+---+---+ | 1 | 1 | 3 | +---+---+---+
โ€บ Output: +---+---+---+----------+ | x | y | z | triangle | +---+---+---+----------+ | 1 | 1 | 3 | No | +---+---+---+----------+
๐Ÿ’ก Note: The sum of the two smaller sides (1+1=2) is not greater than the largest side (3), violating the triangle inequality
example_3.sql โ€” Multiple Rows
$ 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 | +---+---+---+----------+
๐Ÿ’ก Note: First row: 13+15=28 < 30, so No. Second row: all conditions satisfied (10+20>15, 10+15>20, 20+15>10), so Yes

Visualization

Tap to expand
Triangle Inequality Theorem Visualizationโœ“ Valid Trianglex = 5y = 4z = 35+4=9 > 3 โœ“5+3=8 > 4 โœ“4+3=7 > 5 โœ“โœ— Invalid Trianglex = 1y = 1z = 31+1=2 < 3 โœ—Cannot connect!Triangle Inequality RuleFor any triangle with sides a, b, c:โ€ข a + b > c (sum of two sides > third side)โ€ข a + c > bโ€ข b + c > a
Understanding the Visualization
1
Measure the Segments
We have three line segments with lengths x, y, and z
2
Test First Condition
Check if segments x and y together can span more than segment z
3
Test Remaining Conditions
Verify the other two inequality conditions
4
Form Triangle or Reject
If all conditions pass, segments can form a triangle
Key Takeaway
๐ŸŽฏ Key Insight: Three line segments can form a triangle if and only if the sum of any two sides is strictly greater than the third side. This fundamental geometric principle ensures the segments can actually connect to form a closed triangular shape.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all rows in the table

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space needed beyond the result set

n
2n
โœ“ Linear Space

Constraints

  • All side lengths x, y, z are positive integers
  • 1 โ‰ค x, y, z โ‰ค 100
  • The table contains at least 1 row
  • Key Rule: For a valid triangle, sum of any two sides must be strictly greater than the third side
Asked in
Amazon 25 Microsoft 18 Oracle 15 IBM 12
23.5K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen