Check whether every row and column of an n x n matrix contains each number from 1 to n

Quick Overview

Decide whether an n by n integer matrix is valid, meaning every row and every column contains each integer from 1 to n. The problem tests row and column traversal, duplicate detection with a set or boolean array, and keeping the check linear in the number of cells.

Check whether every row and column of an n x n matrix contains each number from 1 to n

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

An `n × n` matrix is considered valid if every row and every column contains all the integers from 1 to `n` (inclusive). You are given an `n × n` integer matrix `matrix`. Return `True` if the matrix is valid, and `False` otherwise. ### Function Signature ```python def check_valid(matrix: list[list[int]]) -> bool: ... ``` ### Rules - `matrix` is given as a list of `n` rows, and each row is a list of `n` integers. - Return a Python `bool`: `True` when every row and every column contains each of 1, 2, ..., `n`, and `False` otherwise. ### Constraints - `n == len(matrix) == len(matrix[i])` - `1 <= n <= 125` - `1 <= matrix[i][j] <= n` ### Examples ```text Input: matrix = [[1, 4, 2, 3], [3, 2, 4, 1], [4, 3, 1, 2], [2, 1, 3, 4]] Output: True ``` Here `n = 4`, and every row and every column holds 1, 2, 3 and 4 exactly once, so the matrix is valid. ```text Input: matrix = [[2, 2, 2], [1, 3, 2], [3, 1, 1]] Output: False ``` The first row is `[2, 2, 2]`, which is missing 1 and 3. One failing row is enough to make the matrix invalid. ```text Input: matrix = [[1, 2], [1, 2]] Output: False ``` Both rows contain 1 and 2, but the first column is `[1, 1]` and the second column is `[2, 2]`, so the columns fail.

Overview: Decide whether an n by n integer matrix is valid, meaning every row and every column contains each integer from 1 to n. The problem tests row and column traversal, duplicate detection with a set or boolean array, and keeping the check linear in the number of cells.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

An n × n matrix is considered valid if every row and every column contains all the integers from 1 to n (inclusive).

You are given an n × n integer matrix matrix. Return True if the matrix is valid, and False otherwise.

Function Signature

def check_valid(matrix: list[list[int]]) -> bool:
    ...

Rules

  • matrix is given as a list of n rows, and each row is a list of n integers.
  • Return a Python bool : True when every row and every column contains each of 1, 2, ..., n , and False otherwise.

Constraints

  • n == len(matrix) == len(matrix[i])
  • 1 <= n <= 125
  • 1 <= matrix[i][j] <= n

Examples

Input: matrix = [[1, 4, 2, 3], [3, 2, 4, 1], [4, 3, 1, 2], [2, 1, 3, 4]]
Output: True

Here n = 4, and every row and every column holds 1, 2, 3 and 4 exactly once, so the matrix is valid.

Input: matrix = [[2, 2, 2], [1, 3, 2], [3, 1, 1]]
Output: False

The first row is [2, 2, 2], which is missing 1 and 3. One failing row is enough to make the matrix invalid.

Input: matrix = [[1, 2], [1, 2]]
Output: False

Both rows contain 1 and 2, but the first column is [1, 1] and the second column is [2, 2], so the columns fail.