NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

Table: transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| transaction_id | integer |
| product_id     | integer |
| user_id        | integer |
+----------------+---------+

Each row records one product in one transaction. A transaction may contain multiple products.

Find the product pair or pairs that are most frequently purchased together in the same transaction. A pair counts once for each transaction containing both products. Return only the pair or pairs whose purchase_count equals the highest frequency; include all tied pairs.

Treat pairs as unordered and require product1 < product2, so (A, B) and (B, A) are represented only once. Output product1, product2, and purchase_count, ordered by product1 and product2.

Example

Input:

transactions table:
+----------------+------------+---------+
| transaction_id | product_id | user_id |
+----------------+------------+---------+
| 1              | 1          | 1       |
| 1              | 2          | 1       |
| 1              | 3          | 1       |
| 2              | 1          | 2       |
| 2              | 2          | 2       |
| 3              | 2          | 3       |
| 3              | 3          | 3       |
+----------------+------------+---------+

Output:

+----------+----------+----------------+
| product1 | product2 | purchase_count |
+----------+----------+----------------+
| 1        | 2        | 2              |
| 2        | 3        | 2              |
+----------+----------+----------------+

Explanation:
Transaction 1 contains products {1,2,3}, so it contributes pairs (1,2), (1,3), and (2,3). Transaction 2 contributes (1,2), while transaction 3 contributes (2,3). The highest frequency is 2, reached by (1,2) and (2,3), so both tied pairs are returned. Pair (1,3), with a frequency of 1, is excluded.


Accepted
8/22
Acceptance Rate36.4%
Test Cases
Input
-
Output
-
Expected
-