FastPrepValidate Rows and Columns

Validate Rows and Columns

WhatNot logoWhatNot● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

You are given a nonempty square integer matrix grid with n rows and n columns. Return true if every row and every column is valid; otherwise return false.

For this exercise, a valid row or column contains every integer from 1 through n exactly once. A value outside that range makes the grid invalid.

The grid is fully filled. Check rows and columns only; there is no subgrid or diagonal requirement.

Function

isValidGrid(grid: int[][]) → boolean

Examples

Example 1

grid = [[1,2],[2,1]]return = true

Every row and column contains 1 and 2 exactly once.

Example 2

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

The rows are valid, but each column repeats a value.

Example 3

grid = [[0]]return = false

A one-cell grid must contain 1; 0 is outside its allowed range.

Constraints

  • For this exercise, assume 1 <= n <= 50.
  • grid.length == n and grid[i].length == n.
  • For this exercise, assume -10^9 <= grid[i][j] <= 10^9.

More WhatNot problems

See WhatNot hiring insights
public boolean isValidGrid(int[][] grid) {
    // write your code here
}
grid[[1,2],[2,1]]
expectedtrue
Checking account…