You are given a list of transactions, where each transaction is a list of products purchased together.
Return the pair of products that appears most frequently across all transactions.
Rules:
- A pair consists of exactly two distinct products.
(A, B)and(B, A)are considered the same pair — always return it in lexicographic order. - If multiple pairs have the same highest frequency, return the lexicographically smallest pair.
- Duplicate items within a transaction should be counted only once.
- Each transaction can contribute multiple pairs.
Examples
01 · Example 1
transactions = [["A","B"],["A","B"],["A","C"]] return = ["A","B"]
Pairs across all transactions: AB appears in transaction 1 and 2 (count=2), AC appears in transaction 3 (count=1). Most frequent pair is (A, B). Return ["A","B"].
02 · Example 2
transactions = [["A","B","C"],["A","B"],["B","C"],["A","C"]] return = ["A","B"]
Transaction 1 contributes pairs AB, AC, BC. Transactions 2–4 contribute AB, BC, AC respectively. Counts: AB=2, AC=2, BC=2 — all tied. Return the lexicographically smallest pair: ["A","B"].
Constraints
1 <= transactions.length <= 10^31 <= transactions[i].length <= 100More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public String[] mostCommonProductPair(String[][] transactions) {
// write your code here
}
transactions[["A","B"],["A","B"],["A","C"]]
expected["A", "B"]
sign in to submit