Problem · Hash Table

Most Common Product Pair

EasyAmazonINTERNOA
See Amazon hiring insights

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^3
  • 1 <= transactions[i].length <= 100
  • Each product name is a non-empty string of uppercase letters.
  • More Amazon problems
    drafts saved locally
    public String[] mostCommonProductPair(String[][] transactions) {
      // write your code here
    }
    
    transactions[["A","B"],["A","B"],["A","C"]]
    expected["A", "B"]
    sign in to submit