Skip to content

41. Customers Who Bought Every Product

Difficulty: Hard · Topics: Joins, Aggregations, Filtering & Selection

Return the customer_id of every customer who bought all products listed in products. purchases may contain repeat purchases and products that are not in the catalogue (ignore those).

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_id
1a
1b
1c
2a
2b
3a
3b
3c
3a

products

product_id
a
b
c

Expected output

customer_id
1
3

Hints

Hint 1This is "relational division": compare how many distinct catalogue products each customer bought with the catalogue size.
Hint 2Keep only catalogue products with a left_semi join.
Hint 3products.count() is a plain number you can compare against.

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