Skip to main content
All Questions

Copy a Spiral Matrix

Medium
Unlock detailed company stats for this questionUpgrade

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]