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 Rust
fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut result = Vec::new();
if matrix.is_empty() { return result; }
let (mut top, mut bottom) = (0, matrix.len() - 1);
let (mut left, mut right) = (0, matrix[0].len() - 1);
while left <= right && top <= bottom {
for i in left..=right {
result.push(matrix[top][i]);
}
top += 1;
for i in top..=bottom {
result.push(matrix[i][right]);
}
right -= 1;
if top <= bottom {
for i in (left..=right).rev() {
result.push(matrix[bottom][i]);
}
bottom -= 1;
}
if left <= right {
for i in (top..=bottom).rev() {
result.push(matrix[i][left]);
}
left += 1;
}
}
result
}
fn main() {
let matrix = vec![
vec![1, 2, 3],
vec![4, 5, 6],
vec![7, 8, 9],
];
let output = spiral_order(matrix);
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Traverse the matrix edges in clockwise order, shrinking the boundaries after each edge.
✗ Incorrect
The spiral traversal starts from the top row left to right, then right column top to bottom, bottom row right to left, and left column bottom to top, repeating until all elements are visited.
❓ Predict Output
intermediateSpiral Traversal Output for 2x4 Matrix
What is the output of the spiral traversal on this 2x4 matrix?
[[1, 2, 3, 4], [5, 6, 7, 8]]
[[1, 2, 3, 4], [5, 6, 7, 8]]
DSA Rust
fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut result = Vec::new();
if matrix.is_empty() { return result; }
let (mut top, mut bottom) = (0, matrix.len() - 1);
let (mut left, mut right) = (0, matrix[0].len() - 1);
while left <= right && top <= bottom {
for i in left..=right {
result.push(matrix[top][i]);
}
top += 1;
for i in top..=bottom {
result.push(matrix[i][right]);
}
right -= 1;
if top <= bottom {
for i in (left..=right).rev() {
result.push(matrix[bottom][i]);
}
bottom -= 1;
}
if left <= right {
for i in (top..=bottom).rev() {
result.push(matrix[i][left]);
}
left += 1;
}
}
result
}
fn main() {
let matrix = vec![
vec![1, 2, 3, 4],
vec![5, 6, 7, 8],
];
let output = spiral_order(matrix);
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Remember to traverse the top row, then right column, then bottom row in reverse, then left column in reverse.
✗ Incorrect
The spiral traversal visits elements in order: top row left to right, right column top to bottom, bottom row right to left, and left column bottom to top.
🔧 Debug
advancedIdentify the Error in Spiral Traversal Code
What error will this Rust code produce when run on a non-empty matrix?
DSA Rust
fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut result = Vec::new();
let (mut top, mut bottom) = (0, matrix.len());
let (mut left, mut right) = (0, matrix[0].len());
while left <= right && top <= bottom {
for i in left..=right {
result.push(matrix[top][i]);
}
top += 1;
for i in top..=bottom {
result.push(matrix[i][right]);
}
if top <= bottom {
for i in (left..=right).rev() {
result.push(matrix[bottom][i]);
}
bottom -= 1;
}
if left <= right {
for i in (top..=bottom).rev() {
result.push(matrix[i][left]);
}
left += 1;
}
}
result
}Attempts:
2 left
💡 Hint
Check how bottom and right are initialized compared to matrix length.
✗ Incorrect
bottom and right are set to matrix.len() and matrix[0].len(), which are out of valid index range (max index is length - 1). Accessing matrix[bottom][i] or matrix[i][right] causes runtime panic.
❓ Predict Output
advancedSpiral Traversal Output for 4x4 Matrix
What is the output of the spiral traversal on this 4x4 matrix?
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
DSA Rust
fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut result = Vec::new();
if matrix.is_empty() { return result; }
let (mut top, mut bottom) = (0, matrix.len() - 1);
let (mut left, mut right) = (0, matrix[0].len() - 1);
while left <= right && top <= bottom {
for i in left..=right {
result.push(matrix[top][i]);
}
top += 1;
for i in top..=bottom {
result.push(matrix[i][right]);
}
right -= 1;
if top <= bottom {
for i in (left..=right).rev() {
result.push(matrix[bottom][i]);
}
bottom -= 1;
}
if left <= right {
for i in (top..=bottom).rev() {
result.push(matrix[i][left]);
}
left += 1;
}
}
result
}
fn main() {
let matrix = vec![
vec![1, 2, 3, 4],
vec![5, 6, 7, 8],
vec![9, 10, 11, 12],
vec![13, 14, 15, 16],
];
let output = spiral_order(matrix);
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Follow the spiral edges carefully, shrinking boundaries after each pass.
✗ Incorrect
The spiral traversal visits the outer edges first, then moves inward layer by layer, collecting elements in clockwise order.
🧠 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 times each element is accessed during traversal.
✗ Incorrect
The algorithm visits each element exactly once in a controlled spiral order, so the total operations scale with the number of elements m*n.
