Magicsheet logo

Minimum Domino Rotations For Equal Row

Medium
12.5%
Updated 8/1/2025

Asked by 4 Companies

Minimum Domino Rotations For Equal Row

1. What is this problem about?

In the Minimum Domino Rotations For Equal Row problem, you have two rows of dominoes, tops and bottoms. Each domino has two values. You want to make all values in either the top row or the bottom row the same. You can rotate a domino (swap its top and bottom values) to achieve this. The goal is to find the minimum number of rotations needed, or return -1 if it's impossible. This problem tests your ability to identify the only possible candidates for the target value.

2. Why is this asked in interviews?

This question is a staple at Microsoft, Meta, and Google because it tests whether a candidate can identify key constraints to reduce the search space. The Minimum Domino Rotations For Equal Row interview question looks like it might require a complex search, but it actually has a very limited number of possibilities. It evaluates your ability to reason about a problem and find a linear time solution.

3. Algorithmic pattern used

The algorithmic pattern used is Greedy/Exhaustive check of limited candidates. The key insight is that if it's possible to make a row uniform, the target value must be one of the values of the first domino (either tops[0] or bottoms[0]). No other value could possibly populate the entire row. We simply check if tops[0] can be the target for the top row or the bottom row, and then do the same for bottoms[0]. This "Array, Greedy interview pattern" is efficient and elegant.

4. Example explanation

Tops: [2, 1, 2, 4, 2, 2] Bottoms: [5, 2, 6, 2, 3, 2]

  1. Candidate 1: target = 2 (from tops[0]).
    • Can we make tops all 2s?
    • At index 1: tops[1]=1, bottoms[1]=2. Swap (1 rotation).
    • At index 3: tops[3]=4, bottoms[3]=2. Swap (1 rotation).
    • All other indices have 2 in either top or bottom.
    • Total rotations to make tops all 2s: 2.
  2. Candidate 2: target = 5 (from bottoms[0]).
    • At index 1: Neither is 5. Impossible for target 5. Minimum rotations = 2.

5. Common mistakes candidates make

In the Minimum Domino Rotations For Equal Row coding problem, a common mistake is trying to count the frequency of all numbers from 1 to 6 and then doing complex math. While this can work, it's often more error-prone than simply checking the first domino's values. Another mistake is not checking both the top and bottom rows for each candidate value. Some candidates also fail to return -1 when no valid rotation sequence exists for any of the candidate values.

6. Interview preparation tip

When you encounter a problem where every "slot" must be filled with the same value, look at the first slot. The target value must come from there. This "Constraint-based Greedy pattern" is a powerful way to turn an apparently complex problem into a few simple checks. Always look for ways to narrow down the "target" early in your analysis.

Similar Questions