Magicsheet logo

Number of Equivalent Domino Pairs

Easy
37.5%
Updated 8/1/2025

Number of Equivalent Domino Pairs

What is this problem about?

The Number of Equivalent Domino Pairs problem gives you a list of dominoes, where each domino is a pair [a, b]. Two dominoes [a,b] and [c,d] are equivalent if (a==c && b==d) or (a==d && b==c). Count the number of equivalent pairs. This Number of Equivalent Domino Pairs coding problem uses normalization and hash map counting.

Why is this asked in interviews?

Amazon, Google, and Bloomberg ask this easy problem to test pair normalization and combination counting. By normalizing each domino (smaller value first), equivalent dominoes map to the same key. For a group of size m, the pairs count = m*(m-1)/2. The array, hash table, and counting interview pattern is demonstrated.

Algorithmic pattern used

Normalize + frequency count + combination formula. For each domino [a,b], normalize to (min(a,b), max(a,b)). Build a frequency map. For each frequency m, add m*(m-1)/2 to the result (number of pairs from the group).

Example explanation

Dominoes: [[1,2],[2,1],[3,4],[5,6],[1,2]]. Normalized: (1,2),(1,2),(3,4),(5,6),(1,2). Frequencies: {(1,2):3, (3,4):1, (5,6):1}. Pairs from (1,2): C(3,2)=3. Others: 0. Total = 3.

Common mistakes candidates make

  • Not normalizing (treating [1,2] and [2,1] as different).
  • Using n*(n+1)/2 instead of n*(n-1)/2 (choosing 2 from n).
  • Iterating pairs explicitly (O(n²)) instead of using the combination formula.
  • Using a string key like "1,2" without consistent ordering.

Interview preparation tip

Pair equivalence problems always require normalization first. The pattern: normalize → count frequencies → sum C(freq,2) for each group. This avoids O(n²) pair checking entirely. The combination formula C(m,2) = m*(m-1)/2 is used whenever you need to count unordered pairs within a frequency group. Practice "count equivalent pairs" problems with different normalization schemes (sort, min/max, canonical form) to build this recognition quickly.

Similar Questions