Skip to content

Practice Find Category 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.

Hard#3554
LeetCode Problem

Find Category Recommendation Pairs

Problem Statement

Tables: ProductPurchases(user_id INT, product_id INT, quantity INT) and ProductInfo(product_id INT, category VARCHAR, price DECIMAL). Find different category pairs purchased by at least three distinct users. Return category1, category2, and customer_count, with category1 lexicographically smaller than category2. Sort by customer_count descending, then both category names ascending. A user counts once even when buying several products in either category. Practice approach: Join products to purchases and first deduplicate (user,category). Self-join those pairs on user with ordered distinct categories, then count users. Deduplication prevents multiple products from exaggerating co-purchase popularity.

Source: Find Category Recommendation Pairs on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
{
  "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
    }
  ]
}
Output
[
  {
    "category1": "Books",
    "category2": "Games",
    "customer_count": 4
  }
]
Explanation

All four users bought from Books and Games. User 1 contributes once despite purchasing two Books products.

Constraints

ProductPurchases has key (user_id, product_id).
ProductInfo has key product_id.

Related LeetCode Problems

Practice more problems similar to Find Category Recommendation Pairs.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.