Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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));
deleted 6 characters in body
Source Link
konijn
  • 34.3k
  • 5
  • 71
  • 267

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));
Improve formatting, add tags
Source Link
Phrancis
  • 20.5k
  • 6
  • 69
  • 155

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));
Source Link
NinjaG
  • 2.6k
  • 2
  • 29
  • 61
Loading
default

AltStyle によって変換されたページ (->オリジナル) /