FastPrepValidate a Latin Square

Validate a Latin Square

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Problem statement

You are given an n by n integer matrix. A Latin square contains every integer from 1 through n exactly once in each row and exactly once in each column.

Return true if the matrix is a Latin square, otherwise return false. Diagonals have no additional requirement.

Function

isLatinSquare(matrix: int[][]) → boolean

Examples

Example 1

matrix = [[1,2,3],[2,3,1],[3,1,2]]return = true

Each row and each column contains 1, 2, and 3 exactly once.

Example 2

matrix = [[1,2],[1,2]]return = false

Both rows contain the required values, but each column repeats a value.

Example 3

matrix = [[2]]return = false

A Latin square of size 1 must contain 1.

Constraints

  • 1 <= n == matrix.length <= 200.
  • Every row has exactly n elements.
  • -10^9 <= matrix[i][j] <= 10^9.

More Google problems

See Google hiring insights
Saved
public boolean isLatinSquare(int[][] matrix) {
    // Write your code here.
}
matrix[[1,2,3],[2,3,1],[3,1,2]]
expectedtrue
Checking account…