Can These Braces Form a Triangle?

A carpentry shop cuts three boards for every triangular roof brace. Before gluing, the foreman wants to know which sets of cuts can actually form a triangle. Three lengths form a triangle only if each length is strictly less than the sum of the other two. Boards that only lie flat (for example 10, 20, 30) do not count.

Return one row per cut set with the columns set_id, board_a, board_b, board_c, forms_triangle, where forms_triangle is the text 'Yes' or 'No'. Rows may be in any order; an empty table gives no rows.

Tables

brace_cuts — One row per set of three boards cut for a brace.

ColumnTypeDescription
set_id (key)INTEGERUnique id of the cut set
board_aINTEGERLength of the first board in millimetres (positive)
board_bINTEGERLength of the second board in millimetres (positive)
board_cINTEGERLength of the third board in millimetres (positive); the longest board can be in any column

Example

brace_cuts

set_idboard_aboard_bboard_c
1300400500
2100200300
3250250250
4900200300
5120500410
65060200

Expected output

set_idboard_aboard_bboard_cforms_triangle
1300400500Yes
2100200300No
3250250250Yes
4900200300No
5120500410Yes
65060200No

Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.

SQL Path · Strings & CASE · problem 51 of 70 · SQLite

Write the rule as three comparisons: a + b > c, a + c > b and b + c > a.
Strict inequality: equal sums mean the boards lie flat.
Alternatively: the sum of all three must exceed twice the longest board.

The idea

The triangle inequality says three lengths form a triangle only if every pair together is longer than the third. Translate that literally into a CASE expression with three comparisons joined by AND, returning 'Yes' or 'No' for every row.

Two details catch people out. First, the comparison must be strict: when a + b = c the boards lie in a straight line. Second, the longest board can sit in any column, so checking only a + b > c is not enough.

The alternative uses a neat equivalence. Only the longest side can break the rule, and "longest < sum of the other two" is the same as "total > 2 × longest". SQLite's multi-argument scalar max(a, b, c) returns the largest of its arguments, so one comparison does the job.

Solution

SELECT set_id,
       board_a,
       board_b,
       board_c,
       CASE
         WHEN board_a + board_b > board_c
          AND board_a + board_c > board_b
          AND board_b + board_c > board_a THEN 'Yes'
         ELSE 'No'
       END AS forms_triangle
FROM brace_cuts;

Another way

SELECT set_id,
       board_a,
       board_b,
       board_c,
       iif(board_a + board_b + board_c > 2 * max(board_a, board_b, board_c),
           'Yes', 'No') AS forms_triangle
FROM brace_cuts;

Why common answers fail

Uses >= so flat sets such as 10, 20, 30 are wrongly reported as triangles.

SELECT set_id, board_a, board_b, board_c, CASE WHEN board_a + board_b >= board_c AND board_a + board_c >= board_b AND board_b + board_c >= board_a THEN 'Yes' ELSE 'No' END AS forms_triangle FROM brace_cuts;

Checks only one of the three inequalities, assuming the longest board is always board_c.

SELECT set_id, board_a, board_b, board_c, CASE WHEN board_a + board_b > board_c THEN 'Yes' ELSE 'No' END AS forms_triangle FROM brace_cuts;

What the interviewer is checking

A warm-up to see whether you translate a rule into CASE carefully, including strict boundaries and symmetric conditions.

Mistakes to avoid

  • Using >= and accepting flat sets.
  • Testing only one inequality.
  • Confusing the scalar max(a, b, c) with the aggregate MAX(column).

Follow-ups to expect

  • Classify each triangle as equilateral, isosceles or scalene (more WHEN branches, checked in the right order).
  • Count how many sets fail (SUM over the CASE).
  • What changes if lengths could be NULL? (Comparisons become NULL and fall to ELSE; decide whether that should be 'No' or NULL.)

In real work

Rule-based labelling with CASE is everyday work: flagging invalid measurements, eligibility checks and data-quality columns.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url