Coding exercise-Matrix Spiral

Figooooo
3 min readOct 25, 2021

Today, I will write down some thoughts about an exercise that I met during recent practices, matrix spiral.

The requirement is pretty straightforward: write a function that accepts an integer N and returns an N*N spiral matrix.

What does it mean? Let’s see some examples.

matrix(2) should return:

[[1, 2],

[4, 3]]

matrix(3) should return:

[[1, 2, 3],

[8, 9, 4],

[7, 6, 5]]

matrix(4) should return:

[[1, 2, 3, 4],

[12, 13, 14, 5],

[11, 16, 15, 6],

[10, 9, 8, 7]]

As you can see, the matrix function should return an array with n array elements in it. Every array inside this array has the length of n. And the then number in the nested array is incrementing from top left to top right, top right to bottom right, bottom right to bottom left, bottom left to top left just like a spiral.

So the first step is pretty simple, we just need to generate the arrays in the outer array.

const result = []

for(let i = 0; i < n; i++){

result.push([])}

What’s next? How are we gonna put the actual value into different arrays in ascending order? Here is a diagram of how we can implement it.

Since we’re incrementing the value clockwise, we can change the value of the start column, end column, start row, and end row to adjust which element exactly we’re dealing with.

Firstly we need to declare all the variables we need to get this going.

let counter = 1;

let startColumn = 0;

let endColumn = n-1;

let startRow = 0;

let endRow = n — 1

As long as (start column ≤ end column) AND (start row ≤ end row), loop from start column to end column, assign counter variable at result[startrow][i] and increment the counter. We will have the first row piled up.

Because we’re going through the first element to the last element in the outer array on every array’s last index, so we want to increment the start row and loop from start row to end row, assign the counter variable at result[i][endcolumn] and increment the counter.

After that, we’ve completed two sides of our spiral. And we want to decrement the end column so that we’re looping from end column to start column, assign the counter variable at result[endrow][i] and increment the counter.

Lastly, the only thing we have to do is increment the end row and loop from bottom to top, assign the counter variable at result[startcolumn][i] and increment the counter and increment the start column.

To put them into code:

while(startColumn <= endColumn && startRow <= endRow) {

//top

for(let i = startColumn; i <= endColumn; i++) {

result[startRow][i] = counter

counter ++

}

startRow ++

//right

for(let i = startRow; i <= endRow; i++) {

result[i][endColumn] = counter

counter ++

}

endColumn —

//bottom

for(let i = endColumn; i >= startColumn; i — ) {

result[endRow][i] = counter

counter ++

}

endRow —

//left

for(let i = endRow; i >= startRow; i — ) {

result[i][startColumn] = counter

counter ++

}

startColumn ++

}

Don’t forget to return the result!

--

--