Fewest domino rotations to make the top or bottom row a single value

Quick Overview

Given the top and bottom halves of a row of dominoes, find the fewest rotations that make every top or every bottom show the same number, or report that it cannot be done. It tests narrowing down the candidate values and counting rotation costs in a single linear pass.

Fewest domino rotations to make the top or bottom row a single value

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A row of dominoes is given as two arrays: `tops[i]` and `bottoms[i]` are the numbers on the top and bottom halves of the `i`-th domino. Each half shows a number from 1 to 6. Rotating the `i`-th domino swaps `tops[i]` and `bottoms[i]`. Return the minimum number of rotations needed so that all the values in `tops` are the same, or all the values in `bottoms` are the same. If neither can be done, return `-1`. ### Function Signature ```python def min_domino_rotations(tops: list[int], bottoms: list[int]) -> int: pass ``` ### Rules - Either row may be the one that ends up uniform, with any value from 1 to 6. Return the smallest rotation count over all of these options. - A domino is rotated only when the needed value is on the other half. A domino with the same value on both halves never needs a rotation for that value. - Dominoes are indexed from 0. ### Constraints - `2 <= len(tops) <= 2 * 10^4` - `len(bottoms) == len(tops)` - `1 <= tops[i], bottoms[i] <= 6` ### Examples ```text Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2] Output: 2 ``` Every domino has a 2 on at least one half, and no other value does. Making the top row all 2s means rotating dominoes 1 and 3 (2 rotations). Making the bottom row all 2s means rotating dominoes 0, 2 and 4 (3 rotations). The minimum is 2. ```text Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4] Output: -1 ``` Domino 0 shows 3 on both halves, so 3 is the only value that could fill a row. Domino 1 shows 5 and 6, so no row can be made uniform. ```text Input: tops = [1,2,5,2], bottoms = [2,3,2,2] Output: 1 ``` Only 2 appears on every domino. The top row needs dominoes 0 and 2 rotated (2 rotations), while the bottom row needs only domino 1 rotated (1 rotation).

Overview: Given the top and bottom halves of a row of dominoes, find the fewest rotations that make every top or every bottom show the same number, or report that it cannot be done. It tests narrowing down the candidate values and counting rotation costs in a single linear pass.

Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

A row of dominoes is given as two arrays: tops[i] and bottoms[i] are the numbers on the top and bottom halves of the i-th domino. Each half shows a number from 1 to 6.

Rotating the i-th domino swaps tops[i] and bottoms[i]. Return the minimum number of rotations needed so that all the values in tops are the same, or all the values in bottoms are the same. If neither can be done, return -1.

Function Signature

def min_domino_rotations(tops: list[int], bottoms: list[int]) -> int:
    pass

Rules

  • Either row may be the one that ends up uniform, with any value from 1 to 6. Return the smallest rotation count over all of these options.
  • A domino is rotated only when the needed value is on the other half. A domino with the same value on both halves never needs a rotation for that value.
  • Dominoes are indexed from 0.

Constraints

  • 2 <= len(tops) <= 2 * 10^4
  • len(bottoms) == len(tops)
  • 1 <= tops[i], bottoms[i] <= 6

Examples

Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2

Every domino has a 2 on at least one half, and no other value does. Making the top row all 2s means rotating dominoes 1 and 3 (2 rotations). Making the bottom row all 2s means rotating dominoes 0, 2 and 4 (3 rotations). The minimum is 2.

Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
Output: -1

Domino 0 shows 3 on both halves, so 3 is the only value that could fill a row. Domino 1 shows 5 and 6, so no row can be made uniform.

Input: tops = [1,2,5,2], bottoms = [2,3,2,2]
Output: 1

Only 2 appears on every domino. The top row needs dominoes 0 and 2 rotated (2 rotations), while the bottom row needs only domino 1 rotated (1 rotation).