Amazon wants to implement the "Customers who bought this also bought..." feature based on co-purchase patterns.
Given two tables:
- ProductPurchases: Records of user purchases with quantities
- ProductInfo: Product details including category and price
Write a solution to:
- Identify distinct product pairs frequently purchased together by the same customers (where
product1_id < product2_id) - For each product pair, determine how many customers purchased both products
A product pair is considered for recommendation if at least 3 different customers have purchased both products.
Return results ordered by customer_count in descending order, then by product1_id ascending, then by product2_id ascending.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | Unique identifier for each user |
product_id
PK
|
int | Unique identifier for each product |
quantity
|
int | Quantity of product purchased by user |
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Unique identifier for each product |
category
|
varchar | Product category |
price
|
decimal | Product price |
Input & Output
| user_id | product_id | quantity |
|---|---|---|
| 1 | 101 | 2 |
| 1 | 102 | 1 |
| 1 | 103 | 3 |
| 2 | 101 | 1 |
| 2 | 102 | 3 |
| 3 | 101 | 2 |
| 3 | 102 | 1 |
| 3 | 104 | 2 |
| 4 | 101 | 1 |
| 4 | 103 | 2 |
| product_id | category | price |
|---|---|---|
| 101 | Electronics | 299.99 |
| 102 | Electronics | 199.99 |
| 103 | Books | 29.99 |
| 104 | Home | 89.99 |
| product1_id | product2_id | customer_count |
|---|---|---|
| 101 | 102 | 3 |
Products 101 and 102 are purchased together by users 1, 2, and 3 (3 customers total), meeting the minimum threshold of 3. Other pairs like (101,103) and (101,104) only have 2 customers each, so they don't qualify.
| user_id | product_id | quantity |
|---|---|---|
| 1 | 201 | 1 |
| 1 | 202 | 2 |
| 2 | 201 | 1 |
| 2 | 202 | 1 |
| 3 | 201 | 3 |
| 3 | 202 | 1 |
| 4 | 201 | 2 |
| 4 | 202 | 1 |
| 1 | 203 | 1 |
| 2 | 203 | 2 |
| 3 | 203 | 1 |
| product_id | category | price |
|---|---|---|
| 201 | Fashion | 79.99 |
| 202 | Fashion | 49.99 |
| 203 | Beauty | 24.99 |
| product1_id | product2_id | customer_count |
|---|---|---|
| 201 | 202 | 4 |
| 201 | 203 | 3 |
Two product pairs qualify: (201,202) with 4 customers and (201,203) with 3 customers. Results are ordered by customer_count descending, so (201,202) appears first.
| user_id | product_id | quantity |
|---|---|---|
| 1 | 301 | 1 |
| 1 | 302 | 2 |
| 2 | 301 | 1 |
| 3 | 302 | 1 |
| 4 | 303 | 2 |
| product_id | category | price |
|---|---|---|
| 301 | Sports | 159.99 |
| 302 | Sports | 89.99 |
| 303 | Toys | 39.99 |
| product1_id | product2_id | customer_count |
|---|
Only user 1 bought both products 301 and 302 together. Since this is less than the required 3 customers, no product pairs qualify for recommendation.
Constraints
-
1 ≤ user_id ≤ 1000 -
1 ≤ product_id ≤ 10000 -
1 ≤ quantity ≤ 100 - At least 3 different customers must purchase both products for recommendation
-
Product pairs must satisfy
product1_id < product2_id