46. Sorted Distinct Purchase List
Difficulty: Medium · Topics: Arrays, Aggregations
For each customer_id in purchases, return products, the distinct products they bought as an array sorted alphabetically, and product_count, the number of distinct products.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
purchases
| customer_id | product |
|---|---|
| 1 | pear |
| 1 | apple |
| 1 | pear |
| 2 | kiwi |
Expected output
| customer_id | products | product_count |
|---|---|---|
| 1 | [apple, pear] | 2 |
| 2 | [kiwi] | 1 |
Hints
Hint 1
F.collect_set gathers distinct values per group, in no guaranteed order.Hint 2
Wrap it inF.sort_array for a deterministic, sorted array.Hint 3
F.size gives the array length.PySpark functions you'll practise
- groupBy
- agg
- collect_set
- sort_array
- size
Related problems
- Word Count · Medium · Arrays
- Fill Missing Dates (Calendar Spine) · Hard · Joins
- Average Salary by Department · Easy · Aggregations
- Departments with Large Teams · Easy · Aggregations
- Three-Day Login Streak · Hard · Window Functions
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings
Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems