Skip to main content

Copy a Spiral Matrix

MediumPremium

Given a 2D array (matrix) inputMatrix of integers, create a function spiralCopy that:

  • Copies inputMatrix’s values into a 1D array in a spiral order, clockwise.
  • Returns the the 1D array.

For example:

input: inputMatrix = [ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20] ] output: [1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12. 2]

What pattern(s) jump out at you when looking at the above image of a spiral clockwise matrix?

If you are tempted to change the matrix - don't. There's no need to do this.

Have you checked for negative or out of bounds indices?

Double check that each value is copied only once!