DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

Spiral Matrix

**Problem Statement:** Given an `m x n` `matrix`, return all elements of the `matrix` in spiral order. **Hint / Expected Approach:** Shrink boundary: top/bottom/left/right **Edge Cases to Consider:** - (1) Single row or column matrix - (2) 1×1 matrix - (3) Non-square matrix

Examples

Example 1
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Example 2
Input: [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Example 3
Input: [[1]]
Output: [1]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪Single row or column matrix
  • ▪1×1 matrix
  • ▪Non-square matrix
Frequently Asked At
GoogleAmazonMicrosoftAdobe