LeetCode Problem

How to Solve Spiral Matrix II

To solve the Spiral Matrix II problem, we need to generate a matrix of size n x n with values from 1 to n² in a spiral order. The challenge involves simulating the traversal through the matrix by iterating in right, down, left, and up directions, ensuring that values are filled correctly without overwriting previously filled cells.

GhostInterview Help

Need help with Spiral Matrix II without spending extra time grinding it?

GhostInterview can read Spiral Matrix II from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #59Array plus MatrixReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array plus Matrix
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Spiral Matrix II problem, we need to generate a matrix of size n x n with values from 1 to n² in a spiral order. The challenge involves simulating the traversal through the matrix by iterating in right, down, left, and up directions, ensuring that values are filled correctly without overwriting previously filled cells.

Problem Statement

Given a positive integer n, your task is to generate an n x n matrix filled with integers from 1 to n² in a spiral order. The matrix must begin by filling the top-left corner, then proceed right, down, left, and up in a spiral until all cells are filled.

For example, for n = 3, the correct output would be [[1, 2, 3], [8, 9, 4], [7, 6, 5]]. For n = 1, the output would be [[1]].

Examples

Example 1

Input: n = 3

Output: [[1,2,3],[8,9,4],[7,6,5]]

Example details omitted.

Example 2

Input: n = 1

Output: [[1]]

Example details omitted.

Constraints

  • 1 <= n <= 20

Solution Approach

Simulate Spiral Traversal

Start from the top-left corner of the matrix and fill numbers from 1 to n². Move in a right, down, left, and up pattern while adjusting the boundaries to avoid overwriting already filled cells.

Boundary Adjustments

After filling each row or column, adjust the boundaries (top, bottom, left, right) to prevent revisiting cells. These adjustments are key to maintaining the correct spiral order.

Efficient Space Usage

Use an in-place matrix filling approach to minimize space complexity. The matrix is filled row by row, and column by column, within a pre-allocated n x n array.

Complexity Analysis

MetricValue
Time\mathcal{O}(n^2)
Space\mathcal{O}(1)

The time complexity is O(n²) due to the need to visit every cell in the n x n matrix exactly once. The space complexity is O(1) as we are using an in-place filling strategy.

What Interviewers Usually Probe

  • The candidate demonstrates an understanding of array traversal techniques.
  • They can describe how to adjust matrix boundaries during traversal.
  • They understand how to optimize for space complexity with an in-place solution.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to adjust the boundaries after completing a row or column, causing an infinite loop or overwriting cells.
  • Not properly handling edge cases, like n = 1 or a very small matrix.
  • Overcomplicating the solution by using extra space when in-place filling is possible.

Follow-up variants

  • Generate the matrix in reverse spiral order, starting from n² and going down to 1.
  • Create a matrix that is filled in a diagonal pattern from top-left to bottom-right.
  • Simulate the matrix traversal in a zigzag pattern, filling values in a back-and-forth manner.

How GhostInterview Helps

  • GhostInterview helps simulate the traversal logic with step-by-step guidance for the spiral pattern.
  • It assists in breaking down the matrix filling process into smaller, manageable steps.
  • GhostInterview provides hints on how to adjust boundaries and optimize space usage during traversal.

Topic Pages

FAQ

What is the time complexity of Spiral Matrix II?

The time complexity is O(n²) since each element in the n x n matrix must be visited once.

How can I handle the matrix boundaries during traversal?

Adjust the boundaries (top, bottom, left, right) after completing each row or column to prevent overwriting or revisiting cells.

Can I use extra space for the matrix in this problem?

The problem specifies an in-place solution, meaning no extra space should be used apart from the matrix itself.

What happens if n = 1?

For n = 1, the matrix is simply [[1]]. The solution still works correctly with this edge case.

How does the spiral pattern work in this problem?

The pattern starts from the top-left corner and moves right, down, left, and up in a continuous loop, filling numbers in this order until the matrix is complete.

GhostInterview Solver

Need direct help with Spiral Matrix II instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Spiral Matrix II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.