FastPrepValidate a Latin-Square Matrix

Validate a Latin-Square Matrix

Karat logoKarat● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a nonempty square integer matrix of size N × N, return whether every row and every column contains each integer from 1 through N exactly once.

For this exercise, assume the row and column conditions must both hold. This follows the reported examples: a matrix whose rows are all [1,2,3] is invalid because its columns repeat values.

Values outside [1,N] make the matrix invalid. The matrix need not have any particular diagonal or arrangement beyond the row and column conditions.

Function

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

Examples

Example 1

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

Every row and column contains exactly 1, 2 and 3.

Example 2

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

Each row is valid, but columns repeat one value and omit others.

Example 3

matrix = [[1000,2,0],[-1000,2,3],[1,2,3]]return = false

Several values lie outside 1 through 3, so the matrix is invalid.

Constraints

  • For this exercise, assume 1 <= N <= 30.
  • The input has exactly N rows of exactly N integers.
  • For this exercise, assume each integer lies in [-10^9,10^9].

More Karat problems

See Karat hiring insights
public boolean validateLatinSquare(int[][] matrix) {
    // Write your code here.
    return false;
}
matrix[[1,3,2],[2,1,3],[3,2,1]]
expectedtrue
Checking account…