Percentage of Total
PySpark data engineering interview problem. Difficulty: intermediate. Pattern: Aggregation. About 16 minutes. Part of the Pro drill bank.
Each user's spend as a percent of all spend. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A percentage of total shows how big each part is compared to the whole. Here, the whole is the money spent by all users, and each part is one user's spending. Add up amount for each user_id as total_spend. Then find the grand total across all users and calculate pct_of_total as 100 * total_spend / grand_total, rounded to 2 decimals. Return user_id, total_spend and pct_of_total, sorted by user_id. The editor creates the table df and ends with result = df. Change the last line to build your answer. You need the grand total on every row, so you can join it back, or use a window with no partition.
Requirements
- Compute total_spend as the sum of amount for each user.
- Compute the grand total of all total_spend values.
- Add pct_of_total as round(100 * total_spend / grand_total, 2).
- Return user_id, total_spend, pct_of_total, sorted by user_id, in result.
Constraints
- The grand total is the sum over all rows, not over one user.
- Multiply by 100.0 so the division gives decimals.
- Round to 2 decimals with F.round(..., 2).
Examples
Input: spend rows Output: user_id | total_spend | pct_of_total 1 | 40.0 | 36.36 2 | 20.0 | 18.18 3 | 50.0 | 45.45 Grand total is 110.
Topics: lakebench, pyspark, ratio, sum.
More PySpark interview questions · All interview problems · Learn data engineering
Percentage of Total
Interview-style drill: Each user's spend as a percent of all spend.
Sum `amount` per `user_id` as `total_spend`, then compute `pct_of_total` = round(100 * total_spend / grand_total, 2). Order by `user_id`. Assign `result`.