Find Product Recommendation Pairs - Problem
Amazon's "Customers who bought this item also bought" feature is one of the most powerful recommendation systems in e-commerce! ๐
You're tasked with building the core algorithm that identifies product pairs that are frequently purchased together by the same customers. This classic market basket analysis problem helps Amazon suggest relevant products and increase sales.
Given:
ProductPurchasestable: Contains user purchases with (user_id, product_id, quantity)ProductInfotable: Contains product details with (product_id, category, price)
Goal: Find all product pairs where at least 3 different customers have purchased both products. For each qualifying pair, count how many customers bought both items.
Output Requirements:
- Return pairs as (product1_id, product2_id) where product1_id < product2_id
- Include the customer count for each pair
- Sort by customer_count DESC, then product1_id ASC, then product2_id ASC
Input & Output
example_1.sql โ Basic Product Pairs
$
Input:
ProductPurchases:
| user_id | product_id | quantity |
|---------|------------|-----------|
| 1 | 101 | 2 |
| 1 | 102 | 1 |
| 2 | 101 | 1 |
| 2 | 102 | 3 |
| 3 | 101 | 1 |
| 3 | 102 | 2 |
| 4 | 103 | 1 |
ProductInfo:
| product_id | category | price |
|------------|-------------|-------|
| 101 | Electronics | 299.99|
| 102 | Electronics | 149.99|
| 103 | Books | 29.99 |
โบ
Output:
| product1_id | product2_id | customer_count |
|-------------|-------------|----------------|
| 101 | 102 | 3 |
๐ก Note:
Products 101 and 102 were purchased together by users 1, 2, and 3 (exactly 3 customers), so this pair qualifies. Product 103 was only bought by user 4, so it doesn't form any qualifying pairs.
example_2.sql โ Multiple Qualifying Pairs
$
Input:
ProductPurchases:
| user_id | product_id | quantity |
|---------|------------|-----------|
| 1 | 101 | 1 |
| 1 | 102 | 1 |
| 1 | 103 | 2 |
| 2 | 101 | 2 |
| 2 | 102 | 1 |
| 3 | 101 | 1 |
| 3 | 102 | 3 |
| 4 | 101 | 1 |
| 4 | 103 | 1 |
| 5 | 102 | 2 |
| 5 | 103 | 1 |
โบ
Output:
| product1_id | product2_id | customer_count |
|-------------|-------------|----------------|
| 101 | 102 | 3 |
| 101 | 103 | 2 |
| 102 | 103 | 2 |
๐ก Note:
Only pair (101,102) qualifies with 3 customers (users 1,2,3). Pairs (101,103) and (102,103) each have only 2 customers, falling short of the 3-customer minimum requirement.
example_3.sql โ Edge Case with Sorting
$
Input:
ProductPurchases:
| user_id | product_id | quantity |
|---------|------------|-----------|
| 1 | 201 | 1 |
| 1 | 202 | 1 |
| 1 | 301 | 1 |
| 2 | 201 | 2 |
| 2 | 202 | 1 |
| 2 | 301 | 2 |
| 3 | 201 | 1 |
| 3 | 202 | 1 |
| 3 | 301 | 1 |
| 4 | 201 | 1 |
| 4 | 301 | 1 |
| 5 | 201 | 2 |
| 5 | 301 | 1 |
โบ
Output:
| product1_id | product2_id | customer_count |
|-------------|-------------|----------------|
| 201 | 301 | 5 |
| 201 | 202 | 3 |
๐ก Note:
Both pairs qualify, but (201,301) has higher customer count (5) so appears first. When counts are equal, we'd sort by product1_id ASC, then product2_id ASC. Here (201,301) comes before (201,202) due to higher customer count.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Shopping Data
Gather all customer purchase records with user IDs and product IDs
2
Generate Product Pairs
For each customer, create all possible product combinations from their purchases
3
Count Co-purchases
Group identical product pairs and count how many different customers bought both
4
Filter & Rank
Keep pairs with 3+ customers and rank by popularity for recommendations
Key Takeaway
๐ฏ Key Insight: Self-join approach efficiently generates product pairs per customer and leverages SQL's GROUP BY to count co-purchases, making it perfect for real-world recommendation systems like Amazon's 'customers who bought this also bought' feature.
Time & Space Complexity
Time Complexity
O(NรP_avgยฒ)
N customers, P_avgยฒ for product pairs per customer on average
โ Linear Growth
Space Complexity
O(Pยฒ)
Storage for unique product pairs in result
โ Linear Space
Constraints
- 1 โค number of users โค 104
- 1 โค number of products โค 103
- 1 โค quantity โค 100
- At least 3 different customers must purchase both products for a pair to qualify
- Product pairs must satisfy product1_id < product2_id condition
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code