54. Spiral Matrix
mediumAsked at HPHP's scanning and imaging pipelines traverse pixel data in structured patterns — raster scans, zigzag patterns, and spiral reads appear in color-calibration and print-head alignment routines. Spiral Matrix tests whether you can maintain boundaries and direction state without off-by-one errors, a critical skill for HP systems engineers.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in HP loops.
- Glassdoor (2025-Q4)— HP SWE onsite feedback includes Spiral Matrix as a boundary-condition question in rounds testing simulation and matrix traversal.
- Blind (2025-08)— HP technical interview threads cite Spiral Matrix in discussions of array manipulation problems for systems-software roles.
Problem
Given an m x n matrix, return all elements of the matrix in spiral order.
Constraints
m == matrix.lengthn == matrix[0].length1 <= m, n <= 10−100 <= matrix[i][j] <= 100
Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]][1,2,3,6,9,8,7,4,5]Explanation: Traverse right along top row, down along right column, left along bottom row, up along left column, then recurse inward.
Example 2
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]][1,2,3,4,8,12,11,10,9,5,6,7]Explanation: Spiral traversal of a non-square matrix.
Approaches
1. Boundary shrinking (four-pointer)
Maintain top, bottom, left, right boundaries. In each iteration, traverse right, down, left, up — then shrink the corresponding boundary. Stop when boundaries cross.
- Time
- O(m * n)
- Space
- O(1) extra
function spiralOrder(matrix) {
const result = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
// Traverse right
for (let c = left; c <= right; c++) result.push(matrix[top][c]);
top++;
// Traverse down
for (let r = top; r <= bottom; r++) result.push(matrix[r][right]);
right--;
// Traverse left (only if there is a remaining row)
if (top <= bottom) {
for (let c = right; c >= left; c--) result.push(matrix[bottom][c]);
bottom--;
}
// Traverse up (only if there is a remaining column)
if (left <= right) {
for (let r = bottom; r >= top; r--) result.push(matrix[r][left]);
left++;
}
}
return result;
}Tradeoff: O(m*n) time, O(1) extra space (excluding output). The guard conditions (top <= bottom, left <= right) inside the loop body prevent double-counting in non-square matrices.
HP-specific tips
HP interviewers pay close attention to the guard conditions inside the loop — without them, a single-row or single-column matrix gets double-counted. Draw a 1×3 and a 3×1 matrix as test cases before writing code. Name your boundaries explicitly (top, bottom, left, right) and update them immediately after each traversal direction. The pattern is applicable to many HP imaging algorithms that require structured 2D traversal.
Common mistakes
- Missing the top <= bottom guard before traversing left — duplicates elements in matrices with a single remaining row.
- Missing the left <= right guard before traversing up — duplicates elements in matrices with a single remaining column.
- Forgetting to increment top and decrement right before checking the guards.
- Off-by-one in the boundary initialization — bottom = matrix.length - 1, right = matrix[0].length - 1 (zero-indexed).
Follow-up questions
An interviewer at HP may pivot to one of these next:
- Generate a spiral matrix filled with 1 to m*n (LC 59).
- Rotate the matrix 90 degrees clockwise in-place (LC 48).
- How would you traverse the matrix in a zigzag (diagonal) order instead of spiral?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why do you need guards for left/right traversal inside the while loop?
After traversing right (top row) and down (right column), if only one row remains, the left traversal would re-traverse that row backward. The top <= bottom guard prevents this double-count.
Does the order of boundary updates matter?
Yes — update top immediately after traversing right, right immediately after traversing down, etc. The next inner loop uses the updated boundary.
How does this handle a 1×1 matrix?
The while condition is satisfied (top=0 ≤ bottom=0, left=0 ≤ right=0). The right traversal adds the single element; top becomes 1, breaking the while loop. Correct result: [matrix[0][0]].
Practice these live with InterviewChamp.AI
Drill Spiral Matrix and other HP interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →