Practice Find Product Recommendation Pairs
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Tables: ProductPurchases(user_id INT, product_id INT, quantity INT) and ProductInfo(product_id INT, category VARCHAR, price DECIMAL). Find product pairs purchased by at least three distinct users. Use product1_id < product2_id, and return both IDs, product1_category, product2_category, and customer_count. Sort by customer_count descending, then the two IDs ascending. Quantity does not multiply the customer count. Practice approach: Self-join purchases on user ID with ordered product IDs, aggregate distinct users per pair, and join each product's category. Filtering the ordered IDs prevents self-pairs and mirrored duplicates. Apply the three-customer threshold after grouping.
Source: Find Product Recommendation Pairs on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
{
"ProductPurchases": [
{
"user_id": 1,
"product_id": 10,
"quantity": 2
},
{
"user_id": 1,
"product_id": 11,
"quantity": 1
},
{
"user_id": 1,
"product_id": 20,
"quantity": 3
},
{
"user_id": 2,
"product_id": 10,
"quantity": 1
},
{
"user_id": 2,
"product_id": 20,
"quantity": 1
},
{
"user_id": 3,
"product_id": 10,
"quantity": 1
},
{
"user_id": 3,
"product_id": 20,
"quantity": 1
},
{
"user_id": 4,
"product_id": 11,
"quantity": 1
},
{
"user_id": 4,
"product_id": 20,
"quantity": 1
}
],
"ProductInfo": [
{
"product_id": 10,
"category": "Books",
"price": 4
},
{
"product_id": 11,
"category": "Books",
"price": 8
},
{
"product_id": 20,
"category": "Games",
"price": 12
}
]
}[
{
"product1_id": 10,
"product2_id": 20,
"product1_category": "Books",
"product2_category": "Games",
"customer_count": 3
}
]Users 1, 2, and 3 bought products 10 and 20. Product 11 shares product 20 with only users 1 and 4.
Constraints
Related LeetCode Problems
Practice more problems similar to Find Product Recommendation Pairs.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.