FastPrepMost Common Product Pair
Problems/Most Common Product Pair

Most Common Product Pair

Amazon logoAmazon● EasyINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

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.

Function

mostCommonProductPair(transactions: String[][]) → String[]

Examples

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"].

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

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public String[] mostCommonProductPair(String[][] transactions) {
      // write your code here
    }

}
Checking account…