Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).
Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.
Example 1:
Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true.
Example 2:
Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.
Constraints:
n == matrix.length == matrix[i].length1 <= n <= 1001 <= matrix[i][j] <= nProblem summary: An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive). Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,2,3],[3,1,2],[2,3,1]]
[[1,1,1],[1,2,3],[1,2,3]]
valid-sudoku)matrix-diagonal-sum)first-completely-painted-row-or-column)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2133: Check if Every Row and Column Contains All Numbers
class Solution {
public boolean checkValid(int[][] matrix) {
int n = matrix.length;
boolean[] vis = new boolean[n + 1];
for (var row : matrix) {
Arrays.fill(vis, false);
for (int x : row) {
if (vis[x]) {
return false;
}
vis[x] = true;
}
}
for (int j = 0; j < n; ++j) {
Arrays.fill(vis, false);
for (int i = 0; i < n; ++i) {
if (vis[matrix[i][j]]) {
return false;
}
vis[matrix[i][j]] = true;
}
}
return true;
}
}
// Accepted solution for LeetCode #2133: Check if Every Row and Column Contains All Numbers
func checkValid(matrix [][]int) bool {
n := len(matrix)
for _, row := range matrix {
vis := make([]bool, n+1)
for _, x := range row {
if vis[x] {
return false
}
vis[x] = true
}
}
for j := 0; j < n; j++ {
vis := make([]bool, n+1)
for i := 0; i < n; i++ {
if vis[matrix[i][j]] {
return false
}
vis[matrix[i][j]] = true
}
}
return true
}
# Accepted solution for LeetCode #2133: Check if Every Row and Column Contains All Numbers
class Solution:
def checkValid(self, matrix: List[List[int]]) -> bool:
n = len(matrix)
return all(len(set(row)) == n for row in chain(matrix, zip(*matrix)))
// Accepted solution for LeetCode #2133: Check if Every Row and Column Contains All Numbers
struct Solution;
impl Solution {
fn check_valid(matrix: Vec<Vec<i32>>) -> bool {
let n = matrix.len();
for i in 0..n {
let mut col = vec![false; n];
for j in 0..n {
let x = matrix[i][j];
if (1..=n as i32).contains(&x) {
let k = (x - 1) as usize;
col[k] = true;
} else {
return false;
}
}
for j in 0..n {
if !col[j] {
return false;
}
}
}
for i in 0..n {
let mut row = vec![false; n];
for j in 0..n {
let x = matrix[j][i];
if (1..=n as i32).contains(&x) {
let k = (x - 1) as usize;
row[k] = true;
} else {
return false;
}
}
for j in 0..n {
if !row[j] {
return false;
}
}
}
true
}
}
#[test]
fn test() {
let matrix = vec_vec_i32![[1, 2, 3], [3, 1, 2], [2, 3, 1]];
let res = true;
assert_eq!(Solution::check_valid(matrix), res);
let matrix = vec_vec_i32![[1, 1, 1], [1, 2, 3], [1, 2, 3]];
let res = false;
assert_eq!(Solution::check_valid(matrix), res);
}
// Accepted solution for LeetCode #2133: Check if Every Row and Column Contains All Numbers
function checkValid(matrix: number[][]): boolean {
const n = matrix.length;
const vis: boolean[] = Array(n + 1).fill(false);
for (const row of matrix) {
vis.fill(false);
for (const x of row) {
if (vis[x]) {
return false;
}
vis[x] = true;
}
}
for (let j = 0; j < n; ++j) {
vis.fill(false);
for (let i = 0; i < n; ++i) {
if (vis[matrix[i][j]]) {
return false;
}
vis[matrix[i][j]] = true;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.