FastPrepInitialize a Playable Match-Three Board

Initialize a Playable Match-Three Board

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Construct a square board with size rows and columns using integer colors from 0 through candyTypes - 1. You may use any subset of the available colors.

A match is a horizontal or vertical run of at least three equal colors. Diagonal runs do not count.

Return any board that satisfies both requirements:

  • The initial board has no match.
  • At least one swap of two horizontally or vertically adjacent cells with different colors creates a match.

Only the initial board and existence of a first legal move are judged. Do not simulate removals, falling candies, or later moves. The examples show possible outputs; any valid board is accepted.

Function

initializeCandyBoard(size: int, candyTypes: int) → int[][]

Examples

Example 1

size = 3candyTypes = 2return = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]

One possible board uses colors 0 and 1. Swapping cells (0,1) and (1,1) creates a horizontal triple in the top row.

Example 2

size = 4candyTypes = 3return = [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]]

A palette of three colors is available, but a valid board may use only two. Any valid board is accepted.

Example 3

size = 5candyTypes = 2return = [[0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 0]]

Alternating both rows and columns avoids initial triples on an odd-sized board.

Constraints

  • 3 <= size <= 100.
  • 2 <= candyTypes <= 100.

More Google problems

See Google hiring insights
public int[][] initializeCandyBoard(int size, int candyTypes) {
    // Write your code here
}
size3
candyTypes2
reference[[0, 1, 0], [1, 0, 1], [0, 1, 0]]
Checking account…