Verify Valid n x n Matrix
Easy
2
44.4% Acceptance
In this lab, you will implement a function that verifies whether an n x n
matrix is valid or not. A matrix is valid if every row and every column contains all the integers from 1
to n
(inclusive).
Problem Statement
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].length
1 <= n <= 100
1 <= matrix[i][j] <= n
Challenges
- Export the
isValidMatrix
function fromindex.js
. - Implement the
isValidMatrix
function to check if the matrix is valid according to the above problem statement.
Evaluation script
import fs from 'fs' const testlog = [] // Challenge 1 try { const isValidMatrix = await import('/home/damner/code/index.js').then(t => t.isValidMatrix) if(isValidMatrix != undefined) { testlog.push({ status: 'pass' }) } else { throw new Error('Function is not exported') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge failed' }) } // Challenge 2 try { const isValidMatrix = await import('/home/damner/code/index.js').then(t => t.isValidMatrix) const matrix1 = [[1, 2, 3], [3, 1, 2], [2, 3, 1]] const matrix2 = [[1, 1, 1], [1, 2, 3], [1, 2, 3]] if(isValidMatrix(matrix1) === true && isValidMatrix(matrix2) === false) { testlog.push({ status: 'pass' }) } else { throw new Error('Function implementation incorrect') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge failed' }) } fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))
Setting up test environment script
#!/bin/bash set -e 1 mkdir -p /home/damner/code/.labtests cat > /home/damner/code/.labtests/package.json << EOF { "type": "module" } EOF cd /home/damner/code/.labtests mv $TEST_FILE_NAME ./nodecheck.test.js # import puppeteer doesn't work without it npm link puppeteer node nodecheck.test.js 2>&1 | tee evaluationscript.log
Initial file system for user
/** * @param {number[][]} matrix * @return {boolean} */ export function isValidMatrix(matrix) { };
{ "name": "codedamn-lab", "type": "module" }
tabs: ['index.js'] terminals: ['yarn install']