EvalFrame Technical Mock Interviews & Coding Practice

Spiral Matrix

Medium · Matrix · Algorithm problem · all problems

Given an `m x n` matrix, return all elements of the matrix in spiral order. **Examples:** ```typescript spiralOrder([[1,2,3],[4,5,6],[7,8,9]]) // [1,2,3,6,9,8,7,4,5] spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) // [1,2,3,4,8,12,11,10,9,5,6,7] ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(m * n)
  • Space complexity: O(1)

Every cell in the matrix is visited exactly once.

Follow-Up Questions Interviewers Ask About Spiral Matrix

  • Why are the additional if (top <= bottom) and if (left <= right) checks required?
    They prevent duplicate traversal on single-row or single-column matrices after top or right has been incremented/decremented.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.