Amazon | Data Engineer II Interview Question - Valid Triangle Detection (SQL & PySpark)



Data engineering interviews at companies like Amazon often include problems that test both logical reasoning and data transformation skills. One such question involves determining whether given values can form a valid triangle. While the problem appears simple at first glance, it requires candidates to demonstrate knowledge of SQL aggregation, pivoting data, and translating logic into scalable solutions using tools like PySpark.

Below is a breakdown of the problem, approach, and solutions typically expected during an interview.

Problem Statement

You are given a table that stores the lengths of triangle sides. The table contains three columns:

CombinationID

SideID (A, B, or C)

SideLength

Each CombinationID represents one potential triangle and contains exactly three rows corresponding to the three sides A, B, and C.

Your task is to return the CombinationIDs that represent valid triangles.

Triangle Validation Rule

For three sides to form a valid triangle, they must satisfy the triangle inequality rule:

A + B > C

A + C > B

B + C > A

If any one of these conditions fails, the three sides cannot form a triangle.

For example, if the sides are 2, 3, and 6:

2 + 3 ≤ 6

This violates the rule, meaning the triangle is invalid.

Understanding the Approach

Before writing any SQL or PySpark code, the key is to understand how the data must be transformed.

Each triangle currently has three rows, one for each side. However, to apply the triangle inequality rule easily, we need one row per CombinationID with the three side lengths as columns.

So the steps are:

➤ Convert rows into columns (pivoting the sides A, B, C).

➤ Apply the triangle inequality conditions.

➤ Return only the CombinationIDs that satisfy all conditions.

This pattern grouping, pivoting, and validating conditions is very common in data engineering interview questions.

SQL Solution

In SQL, the goal is to aggregate rows by CombinationID and extract each side length using conditional logic.

We can use CASE WHEN to convert rows into columns and then apply validation logic using the HAVING clause.

Example SQL solution:

SELECT

   CombinationID

FROM triangles

GROUP BY CombinationID

HAVING

   MAX(CASE WHEN SideID = 'A' THEN SideLength END) +

   MAX(CASE WHEN SideID = 'B' THEN SideLength END) >

   MAX(CASE WHEN SideID = 'C' THEN SideLength END)

AND

   MAX(CASE WHEN SideID = 'A' THEN SideLength END) +

   MAX(CASE WHEN SideID = 'C' THEN SideLength END) >

   MAX(CASE WHEN SideID = 'B' THEN SideLength END)

AND

   MAX(CASE WHEN SideID = 'B' THEN SideLength END) +

   MAX(CASE WHEN SideID = 'C' THEN SideLength END) >

   MAX(CASE WHEN SideID = 'A' THEN SideLength END);

This approach works because:

➤ CASE WHEN extracts side lengths based on SideID.

➤ MAX() collapses the rows into a single row per CombinationID.

➤ The HAVING clause ensures only valid triangles are returned.

This pattern is often seen in SQL interviews when candidates must reshape data before applying validation rules.

PySpark Solution

In real-world data engineering pipelines, similar logic is often implemented using PySpark.

The first step is to pivot the data so each triangle appears as a single row containing columns A, B, and C.

Example PySpark implementation:

from pyspark.sql import SparkSession

from pyspark.sql.functions import col, max

spark = SparkSession.builder.getOrCreate()

data = [

   (1, 'A', 3), (1, 'B', 4), (1, 'C', 5),

   (2, 'A', 2), (2, 'B', 3), (2, 'C', 6),

   (3, 'A', 6), (3, 'B', 7), (3, 'C', 10)

]

df = spark.createDataFrame(data, ["CombinationID", "SideID", "SideLength"])

pivot_df = (

   df.groupBy("CombinationID")

     .pivot("SideID", ["A", "B", "C"])

     .agg(max("SideLength"))

)

Once the data is pivoted, we apply the triangle inequality conditions.

result = pivot_df.filter(

   (col("A") + col("B") > col("C")) &

   (col("A") + col("C") > col("B")) &

   (col("B") + col("C") > col("A"))

).select("CombinationID")

result.show()

The final output returns only the valid triangle combinations.

This approach is scalable and works well in distributed environments where millions of records may need to be validated.

Common Interview Mistakes

Many candidates overcomplicate this problem during interviews.

Common mistakes include performing multiple self-joins, hardcoding side values, or using unnecessary user-defined functions. These approaches increase complexity and reduce scalability.

Interviewers usually expect candidates to recognize the simpler pattern:

Group data → Pivot rows into columns → Apply validation conditions.

Why This Question Matters ?

Although the problem seems mathematical, it tests several important data engineering skills.

It evaluates whether candidates understand how to reshape data, apply group-level validations, and translate logic between SQL and distributed frameworks like Spark.

Questions like this help interviewers assess how well a candidate can think about data transformation and scalable processing, which are essential skills for building real-world data pipelines.

Conclusion

The valid triangle detection problem demonstrates how logical reasoning and data transformation techniques come together in data engineering interviews.

By grouping data, pivoting rows into columns, and applying simple validation rules, candidates can efficiently solve the problem using both SQL and PySpark.

Mastering patterns like these not only helps in interviews but also strengthens the practical intuition required to build scalable data processing systems.