FastPrepFastPrep
Problem Brief

Most Common Product Pair

INTERNOA

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.

1Example 1

Input
transactions = [["A","B"],["A","B"],["A","C"]]
Output
["A","B"]
Explanation
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"].

2Example 2

Input
transactions = [["A","B","C"],["A","B"],["B","C"],["A","C"]]
Output
["A","B"]
Explanation
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

Limits and guarantees your solution can rely on.

  • 1 <= transactions.length <= 10^3
  • 1 <= transactions[i].length <= 100
  • Each product name is a non-empty string of uppercase letters.
  • public String[] mostCommonProductPair(String[][] transactions) {
      // write your code here
    }
    
    Input

    transactions

    [["A","B"],["A","B"],["A","C"]]

    Output

    ["A", "B"]

    Sign in to submit your solution.