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 following JavaScript code that prints the spiral traversal of a 3x3 matrix?
DSA Javascript
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) {
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;
}
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(spiralOrder(matrix));Attempts:
2 left
💡 Hint
Follow the edges of the matrix clockwise, shrinking the boundaries after each side is traversed.
✗ Incorrect
The spiral traversal starts from the top-left corner, moves right, then down, then left, then up, and repeats until all elements are visited. The output matches option A.
❓ Predict Output
intermediateSpiral Traversal Output for 2x4 Matrix
What is the output of the spiral traversal function when applied to this 2x4 matrix?
DSA Javascript
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) {
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;
}
const matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8]
];
console.log(spiralOrder(matrix));Attempts:
2 left
💡 Hint
Remember to traverse the top row, then right column, then bottom row in reverse, then left column upwards.
✗ Incorrect
The spiral traversal collects elements in order: top row left to right, right column top to bottom, bottom row right to left, left column bottom to top. The output matches option A.
🔧 Debug
advancedIdentify the Error in Spiral Traversal Code
The following code attempts to perform spiral traversal on a matrix but produces incorrect output. What is the main logical error?
DSA Javascript
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) {
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;
}
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(spiralOrder(matrix));Attempts:
2 left
💡 Hint
Check the loop condition carefully to ensure all elements are included.
✗ Incorrect
Using < excludes the middle row or column when top equals bottom or left equals right. Changing to <= fixes the issue.
🚀 Application
advancedNumber of Elements in Spiral Traversal
Given a matrix of size m x n, how many elements will the spiral traversal output contain?
Attempts:
2 left
💡 Hint
Spiral traversal visits every element exactly once.
✗ Incorrect
Spiral traversal covers all elements in the matrix, so the count is total elements m multiplied by n.
🧠 Conceptual
expertWhy Use Four Pointers in Spiral Traversal?
Why do we use four pointers (top, bottom, left, right) to perform spiral traversal of a matrix?
Attempts:
2 left
💡 Hint
Think about how the traversal moves inward layer by layer.
✗ Incorrect
The four pointers define the current edges of the matrix layer being traversed. After traversing one side, the corresponding pointer moves inward to avoid revisiting elements.
