GroupBy with Multiple Aggregations

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

Per-user total, average, count, and max amount. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

For each user you want a short summary of their spending. From the table df, which has 1 row per transaction, group the rows by user_id and calculate 4 values for each user: total_amount (the sum of the amounts), avg_amount (the average), txn_count (how many transactions) and max_amount (the biggest single amount). Return 1 row per user with the columns user_id, total_amount, avg_amount, txn_count and max_amount, in that order. Sort by user_id. Save your final table in result.

Requirements

  • Return 1 row for each user_id.
  • Calculate all four aggregates in one agg(...) call.
  • Name the columns exactly total_amount, avg_amount, txn_count and max_amount.
  • Sort by user_id.

Constraints

  • The input table is df with the columns user_id, product and amount.
  • Use groupBy('user_id').agg(...) with F.sum, F.avg, F.count and F.max.
  • Give each aggregate a name with .alias(...).

Examples

Input: user product amounts Output: user_id | total_amount | avg_amount | txn_count | max_amount 1 | 60.0 | 20.0 | 3 | 30.0 2 | 20.0 | 10.0 | 2 | 15.0 3 | 8.0 | 8.0 | 1 | 8.0 Multiple agg expressions share one groupBy.

Topics: lakebench, pyspark, groupBy, agg.

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

beginner

GroupBy with Multiple Aggregations

Interview-style drill: Per-user total, average, count, and max amount.

From `df`, group by `user_id` and compute `total_amount`, `avg_amount`, `txn_count`, `max_amount`. Order by `user_id`. Assign `result`.