I got this question during my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during on of my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]5]]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during on of my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during on of my practice interview.
# Matrices - N Spiral Matrix
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
#### Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
#### Examples:
```
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
Input: 1
Output: [[1]]
myMy solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during on of my practice interview.
# Matrices - N Spiral Matrix
#### Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
#### Examples:
```
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
Input: 1
Output: [[1]]
my solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));
I got this question during on of my practice interview.
Matrices - N Spiral Matrix
Prompt
Given an integer N, output an N x N spiral matrix with integers 1 through N.
###Examples:
Input: 3
Output: [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
Input: 1
Output: [[1]]
My solution is below:
function spiralMatrix(n) {
const matrix = [];
let counter = 1,
rowMin = 0,
rowMax = n - 1,
colMin = 0,
colMax = n - 1;
for (let i = 0; i < n; i++) {
matrix.push(new Array(n).fill(0));
}
while (rowMin <= rowMax && colMin <= colMax) {
for (let col = colMin; col <= colMax; col++) {
matrix[rowMin][col] = counter++;
}
rowMin++;
for (let row = rowMin; row <= rowMax; row++) {
matrix[row][colMax] = counter++;
}
colMax--;
for (let col = colMax; col >= colMin; col--) {
matrix[rowMax][col] = counter++;
}
rowMax--;
for (let row = rowMax; row >= rowMin; row--) {
matrix[row][colMin] = counter++;
}
colMin++;
}
return matrix;
}
console.log(spiralMatrix(10));