Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Spiral Matrix
On this page

Problem Statement

Given a 2D matrix of size m x n, return the 1D array containing all elements of matrix in spiral order.

Examples

Example 1:

  • Input: matrix =
[[1,2,3,4],
 [5,6,7,8], 
 [9,10,11,12],
 [13,14,15,16]]
  • Expected Output: [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
  • Justification: We have traversed the matrix in the spiral order.

Example 2:

  • Input: matrix =
[[10,20,30],
 [40,50,60],
 [70,80,90]]
  • Expected Output: [10,20,30,60,90,80,70,40,50]

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page