Validate a Latin-Square Matrix
Updated
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. For example, 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[][]) → booleanExamples
Example 1
matrix = [[1,3,2],[2,1,3],[3,2,1]]return = trueEvery row and column contains exactly 1, 2 and 3.
Example 2
matrix = [[1,2,3],[1,2,3],[1,2,3]]return = falseEach row is valid, but columns repeat one value and omit others.
Example 3
matrix = [[1000,2,0],[-1000,2,3],[1,2,3]]return = falseSeveral values lie outside 1 through 3, so the matrix is invalid.
Constraints
- For this exercise, assume
1 <= N <= 30. - The input has exactly
Nrows of exactlyNintegers. - For this exercise, assume each integer lies in
[-10^9,10^9].
More Karat interview questions
- Do Two Nodes Share a Proper Ancestor?PHONE SCREEN · Seen Sep 2021
- Parent and Child Counts for Every NodePHONE SCREEN · Seen Sep 2021
- Validate a Nonogram GridPHONE SCREEN · Seen Aug 2020
- Nodes With Zero or One ParentPHONE SCREEN · Seen May 2020
- Lowest Common Ancestor in a Rooted TreeONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
- Validate Rows and ColumnsWhatNot · PHONE SCREENWhatNot interview questions
- Word Chain Jumps in a GridGoogle · PHONE SCREENGoogle interview questions
- Sparse Plant Infection SimulationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Sparse Matrix Addition and MultiplicationPinterest · PHONE SCREENPinterest interview questions
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Sparse Matrix MultiplicationByteDance · PHONE SCREENByteDance interview questions
- Valid SudokuVerkada · PHONE SCREENVerkada interview questions
- Configurable Tic-Tac-Toe GameEliseAI · PHONE SCREEN
- Validate Completed SudokuApple · PHONE SCREENApple interview questions