Magicsheet logo

Triangle Judgement

Easy
25%
Updated 8/1/2025

Triangle Judgement

What is this problem about?

The "Triangle Judgement coding problem" is a foundational SQL task. You are given a table Triangle containing three columns: x, y, and z, representing the lengths of three line segments. Your goal is to determine, for each row, whether these three segments can form a valid triangle. According to the triangle inequality theorem, a triangle can only be formed if the sum of the lengths of any two sides is strictly greater than the length of the third side.

Why is this asked in interviews?

This "Triangle Judgement interview question" is often used in preliminary rounds for data-focused roles at companies like Uber and Meta. It validates that the candidate understands basic geometric principles and can implement them using SQL conditional logic. It's a test of "clean code" in SQL—can you write a query that is both correct and easy to read using IF or CASE statements?

Algorithmic pattern used

The "Database interview pattern" for this problem relies on the CASE or IF expression. For each row, we evaluate the condition: x + y > z AND x + z > y AND y + z > x. If this condition is true, the result is "Yes"; otherwise, it is "No". This is a simple per-row operation with O(n)O(n) time complexity.

Example explanation

Table Data:

  • 13, 15, 30
  • 10, 10, 10 Logic:
  1. Row 1: 13 + 15 = 28. 28 is NOT greater than 30. Result: "No".
  2. Row 2: 10 + 10 = 20. 20 > 10. (All combinations work). Result: "Yes". The output table will have an additional column, usually named triangle, containing these "Yes"/"No" values.

Common mistakes candidates make

A common mistake in the "Triangle Judgement coding problem" is only checking one combination of sides (e.g., x + y > z). You must check all three combinations to ensure the segments can form a triangle regardless of which side is the longest. Another error is using >= instead of >, which is incorrect as the sum must be strictly greater.

Interview preparation tip

For the "Database interview pattern," practice using CASE WHEN for multi-condition branching. It is more standard and portable across different SQL dialects (like MySQL, PostgreSQL, and SQL Server) compared to the IF function.

Similar Questions