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_id | product_id |
|---|---|
| 1 | a |
| 1 | b |
| 1 | c |
| 2 | a |
| 2 | b |
| 3 | a |
| 3 | b |
| 3 | c |
| 3 | a |
products
| product_id |
|---|
| a |
| b |
| c |
Expected output
| customer_id |
|---|
| 1 |
| 3 |
Hints
Hint 1
This is "relational division": compare how many distinct catalogue products each customer bought with the catalogue size.Hint 2
Keep only catalogue products with aleft_semi join.Hint 3
products.count() is a plain number you can compare against.PySpark functions you'll practise
- groupBy
- agg
- count
- countDistinct
- join
- left_semi
- filter
- select
- distinct
Related problems
- Monthly Retention by Cohort · Hard · Joins
- Three-Day Login Streak · Hard · Window Functions
- Fill Missing Dates (Calendar Spine) · Hard · Joins
- Word Count · Medium · Arrays
- Departments with Large Teams · Easy · Aggregations
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