Skip to content

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_idproduct
1pear
1apple
1pear
2kiwi

Expected output

customer_idproductsproduct_count
1[apple, pear]2
2[kiwi]1

Hints

Hint 1F.collect_set gathers distinct values per group, in no guaranteed order.
Hint 2Wrap it in F.sort_array for a deterministic, sorted array.
Hint 3F.size gives the array length.

PySpark functions you'll practise

Related problems

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