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 C++ code that prints the spiral traversal of a 3x3 matrix?
DSA C++
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int top = 0, bottom = 2, left = 0, right = 2; std::vector<int> result; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result.push_back(matrix[top][i]); top++; for (int i = top; i <= bottom; i++) result.push_back(matrix[i][right]); right--; if (top <= bottom) { for (int i = right; i >= left; i--) result.push_back(matrix[bottom][i]); bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result.push_back(matrix[i][left]); left++; } } for (int x : result) std::cout << x << " ";
Attempts:
2 left
💡 Hint
Follow the spiral order: top row left to right, right column top to bottom, bottom row right to left, left column bottom to top.
✗ Incorrect
The code traverses the matrix in a spiral order starting from the top-left corner. It moves right, down, left, and up, shrinking the boundaries after each direction until all elements are visited.
❓ Predict Output
intermediateSpiral Traversal Output for 2x4 Matrix
What is the output of the spiral traversal for this 2x4 matrix?
DSA C++
int matrix[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; int top = 0, bottom = 1, left = 0, right = 3; std::vector<int> result; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result.push_back(matrix[top][i]); top++; for (int i = top; i <= bottom; i++) result.push_back(matrix[i][right]); right--; if (top <= bottom) { for (int i = right; i >= left; i--) result.push_back(matrix[bottom][i]); bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result.push_back(matrix[i][left]); left++; } } for (int x : result) std::cout << x << " ";
Attempts:
2 left
💡 Hint
Remember to move right, down, left, then up, adjusting boundaries each time.
✗ Incorrect
The spiral traversal visits top row left to right, then right column down, then bottom row right to left, and finally left column up, covering all elements.
🔧 Debug
advancedIdentify the Bug in Spiral Traversal Code
The following code is intended to print the spiral traversal of a matrix but produces incorrect output. What is the bug?
DSA C++
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int top = 0, bottom = 2, left = 0, right = 2; std::vector<int> result; while (top < bottom && left < right) { for (int i = left; i <= right; i++) result.push_back(matrix[top][i]); top++; for (int i = top; i <= bottom; i++) result.push_back(matrix[i][right]); right--; for (int i = right; i >= left; i--) result.push_back(matrix[bottom][i]); bottom--; for (int i = bottom; i >= top; i--) result.push_back(matrix[i][left]); left++; } for (int x : result) std::cout << x << " ";
Attempts:
2 left
💡 Hint
Check the loop condition carefully to ensure all elements are included.
✗ Incorrect
Using '<' excludes the last row and column when top equals bottom or left equals right, missing elements. Using '<=' ensures all elements are covered.
🧠 Conceptual
advancedTime Complexity of Spiral Matrix Traversal
What is the time complexity of traversing an MxN matrix in spiral order?
Attempts:
2 left
💡 Hint
Consider how many elements are visited in total.
✗ Incorrect
Each element of the matrix is visited exactly once during the spiral traversal, so the time complexity is proportional to the total number of elements, M times N.
🚀 Application
expertSpiral Traversal Output for Non-Square Matrix 4x3
Given the 4x3 matrix below, what is the spiral traversal output?
DSA C++
int matrix[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; int top = 0, bottom = 3, left = 0, right = 2; std::vector<int> result; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result.push_back(matrix[top][i]); top++; for (int i = top; i <= bottom; i++) result.push_back(matrix[i][right]); right--; if (top <= bottom) { for (int i = right; i >= left; i--) result.push_back(matrix[bottom][i]); bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result.push_back(matrix[i][left]); left++; } } for (int x : result) std::cout << x << " ";
Attempts:
2 left
💡 Hint
Follow the spiral carefully, adjusting boundaries after each direction.
✗ Incorrect
The spiral traversal visits elements in order: top row left to right, right column top to bottom, bottom row right to left, left column bottom to top, repeating until all elements are visited.
