Log In

We would like to write a function that traverses a square matrix in a spiral. The spiral would go from outside to inside, and would go in clockwise order.

For example, given this input:
ABCD
EFGH
IJKL
MNOP

The output of the function would be:
["A","B","C","D","H","L","P","O","N","M","I","E","F","G","K","J"]


However, let's take this step by step. First, let's write a function called that would traverse the outer layer of the matrix.

For this input:
ABCD
EFGH
IJKL
MNOP

The output of the function would be:
["A","B","C","D","H","L","P","O","N","M","I","E"]

This can be implemented with 4 loops for the 4 edges of the matrix to traverse.

Sign Up or Log In to access the code editor and answer the question!
Log In