Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino.
Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
Example 1:
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]] Output: 1
Example 2:
Input: dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]] Output: 3
Constraints:
1 <= dominoes.length <= 4 * 104dominoes[i].length == 21 <= dominoes[i][j] <= 9Problem summary: Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino. Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,2],[2,1],[3,4],[5,6]]
[[1,2],[1,2],[1,1],[1,2],[2,2]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1128: Number of Equivalent Domino Pairs
class Solution {
public int numEquivDominoPairs(int[][] dominoes) {
int[] cnt = new int[100];
int ans = 0;
for (var e : dominoes) {
int x = e[0] < e[1] ? e[0] * 10 + e[1] : e[1] * 10 + e[0];
ans += cnt[x]++;
}
return ans;
}
}
// Accepted solution for LeetCode #1128: Number of Equivalent Domino Pairs
func numEquivDominoPairs(dominoes [][]int) (ans int) {
cnt := [100]int{}
for _, e := range dominoes {
x := e[0]*10 + e[1]
if e[0] > e[1] {
x = e[1]*10 + e[0]
}
ans += cnt[x]
cnt[x]++
}
return
}
# Accepted solution for LeetCode #1128: Number of Equivalent Domino Pairs
class Solution:
def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
cnt = Counter()
ans = 0
for a, b in dominoes:
x = a * 10 + b if a < b else b * 10 + a
ans += cnt[x]
cnt[x] += 1
return ans
// Accepted solution for LeetCode #1128: Number of Equivalent Domino Pairs
impl Solution {
pub fn num_equiv_domino_pairs(dominoes: Vec<Vec<i32>>) -> i32 {
let mut cnt = [0i32; 100];
let mut ans = 0;
for d in dominoes {
let a = d[0] as usize;
let b = d[1] as usize;
let key = if a < b { a * 10 + b } else { b * 10 + a };
ans += cnt[key];
cnt[key] += 1;
}
ans
}
}
// Accepted solution for LeetCode #1128: Number of Equivalent Domino Pairs
function numEquivDominoPairs(dominoes: number[][]): number {
const cnt: number[] = new Array(100).fill(0);
let ans = 0;
for (const [a, b] of dominoes) {
const key = a < b ? a * 10 + b : b * 10 + a;
ans += cnt[key];
cnt[key]++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.