Challenge - 5 Problems
Spiral Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Spiral Traversal on 3x3 Matrix
What is the output of the spiral traversal on the following 3x3 matrix?
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
DSA Typescript
function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}
return result;
}
console.log(spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]));Attempts:
2 left
💡 Hint
Follow the edges of the matrix in a clockwise spiral.
✗ Incorrect
The spiral traversal starts from top-left, moves right, then down, then left, then up, and repeats inward until all elements are visited.
❓ Predict Output
intermediateSpiral Traversal Output for 2x4 Matrix
What is the spiral traversal output for this 2x4 matrix?
[[1, 2, 3, 4], [5, 6, 7, 8]]
[[1, 2, 3, 4], [5, 6, 7, 8]]
DSA Typescript
function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}
return result;
}
console.log(spiralOrder([[1, 2, 3, 4], [5, 6, 7, 8]]));Attempts:
2 left
💡 Hint
Traverse right on top row, then down right column, then left on bottom row.
✗ Incorrect
The spiral traversal covers top row left to right, right column top to bottom, bottom row right to left, and then up left column if any.
🔧 Debug
advancedIdentify the Bug in Spiral Traversal Code
The following code is intended to perform spiral traversal on a matrix. What is the bug causing incorrect output?
DSA Typescript
function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top < bottom && left < right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
return result;
}Attempts:
2 left
💡 Hint
Check the loop condition to ensure all elements are covered.
✗ Incorrect
Using '<' excludes the last row or column when top equals bottom or left equals right, missing elements in the spiral.
❓ Predict Output
advancedSpiral Traversal Output for 4x3 Matrix
What is the spiral traversal output for this 4x3 matrix?
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
DSA Typescript
function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}
return result;
}
console.log(spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]));Attempts:
2 left
💡 Hint
Follow the spiral edges clockwise from top-left inward.
✗ Incorrect
The traversal covers top row left to right, right column top to bottom, bottom row right to left, and left column bottom to top, repeating inward.
🧠 Conceptual
expertTime Complexity of Spiral Matrix Traversal
What is the time complexity of the spiral matrix traversal algorithm for an m x n matrix?
Attempts:
2 left
💡 Hint
Consider how many elements are processed in total.
✗ Incorrect
The algorithm visits each element once, so the time complexity is proportional to the total number of elements m*n.
