Salesforce interview question
Fill an n by n matrix with 1 to n squared in clockwise spiral order
- Company:
- Salesforce
- Type:
- Coding
- Difficulty:
- Medium
- Reported:
- November 18, 2026
Problem
Given a positive integer n, generate an n x n matrix filled with the numbers from 1 to n * n in spiral order: start at the top-left corner, move right along the top row, then down the right column, then left along the bottom row, then up the left column, and continue inward clockwise until every cell is filled.
Example 1
Input: n = 3
Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]]
The outer ring takes 1 to 8 and the center takes 9.
Example 2
Input: n = 1
Output: [[1]]
A single cell.
Constraints
- Not stated; ask the interviewer.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern