Spiral Matrix Traversal

Matrix Traversal Spiral

Problem: Spiral Matrix Traversal

Given a 2D matrix, write a function that returns all elements of the matrix in spiral order.

For example, given the following matrix:

[
  [ 1,  2,  3,  4],
  [ 5,  6,  7,  8],
  [ 9, 10, 11, 12],
  [13, 14, 15, 16]
]

Your function should return:

[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]

Instructions:

  • Write a function to traverse the matrix in spiral order.
  • Consider edge cases such as empty matrix or non-square matrices.
  • You can assume that the matrix is well-formed (i.e., each row has the same number of columns).

Note: This problem can be a fun challenge to celebrate the creativity and problem solving skills that professionals bring to every day of the year. Happy coding on this November day!