Pandas data engineering interview problem. Difficulty: intermediate. Pattern: Pivot. About 16 minutes. Part of the Pro drill bank.
Reshape quarterly columns into rows and compute growth versus the previous quarter per product. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
df has one row per product and one column per quarter: q1, q2, q3, q4 (revenue). Reshape it to one row per product and quarter with columns product, quarter (q1..q4) and revenue, and add growth_pct: the percentage change versus the previous quarter of the same product, rounded to 1 decimal (NULL for q1). Sort by product, then quarter, reset the index. Assign the DataFrame to result.
Input: df product | q1 | q2 | q3 | q4 A | 100 | 120 | 90 | 135 B | 50 | 40 | 60 | 66 Output: product | quarter | revenue | growth_pct A | q1 | 100 | NULL A | q2 | 120 | 20 A | q3 | 90 | -25 A | q4 | 135 | 50 B | q1 | 50 | NULL B | q2 | 40 | -20 B | q3 | 60 | 50 B | q4 | 66 | 10 Product A grows 20% to q2, falls 25% in q3, then grows 50% in q4. Product B's q1 has no growth even though it follows A's q4.
Topics: lakebench, pandas, melt, pct_change, wide to long.
More interview problems · All interview problems · Learn data engineering
Interview-style drill: Reshape quarterly columns into rows and compute growth versus the previous quarter per product.
`df` has one row per `product` and one column per quarter: `q1`, `q2`, `q3`, `q4` (revenue). Reshape it to one row per product and quarter with columns `product`, `quarter` (`q1`..`q4`) and `revenue`, and add `growth_pct`: the percentage change versus the previous quarter of the **same product**, rounded to 1 decimal (NULL for `q1`). Sort by `product`, then `quarter`, reset the index. Assign the DataFrame to `result`.