Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Unpivot (Melt) Table

PySpark data engineering interview problem. Difficulty: beginner. Pattern: Pivot. About 12 minutes. Free to practice.

Melt wide product columns into long date/product/amount rows via union. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Reverse problem 7 without a melt API: from wide, union one select per product column into long form date, product, amount where product is 'A'|'B'|'C'. Drop zero amounts. Order by date, product. Assign result.

Constraints

  • Use union of selects, not stack/melt.
  • Drop amount == 0.

Examples

Input: wide product columns Output: date | product | amount 2024-01-01 | A | 10.0 2024-01-01 | B | 5.0 2024-01-02 | A | 7.0 2024-01-02 | C | 3.0 2024-01-03 | B | 8.0 2024-01-03 | C | 2.0 Each non-zero wide cell becomes one long row.

Topics: lakebench, pyspark, union, melt.

More PySpark interview questions · All interview problems · Learn data engineering

beginner

Unpivot (Melt) Table

Interview-style drill: Melt wide product columns into long date/product/amount rows via union.

Reverse problem 7 without a melt API: from `wide`, union one select per product column into long form `date`, `product`, `amount` where product is `'A'|'B'|'C'`. Drop zero amounts. Order by `date`, `product`. Assign `result`.