What if you could read a matrix like reading a spiral story, without getting lost or repeating steps?
Why Spiral Matrix Traversal in DSA C#?
Imagine you have a big square cake cut into many small pieces arranged in rows and columns. You want to eat the pieces starting from the outer edge and moving inward in a spiral way. Doing this by remembering each piece manually is hard and confusing.
Trying to pick pieces one by one in a spiral order without a clear plan is slow and easy to mess up. You might skip pieces or repeat some because it's hard to track where you are and which direction to go next.
Spiral Matrix Traversal gives a clear step-by-step way to move through the matrix edges and then move inward layer by layer. It keeps track of boundaries and directions, so you never miss or repeat pieces.
int row = 0, col = 0; while (not all elements visited) { // guess next position manually // check if inside bounds // print element // update row and col }
int top = 0, bottom = rows - 1; int left = 0, right = cols - 1; while (top <= bottom && left <= right) { // traverse from left to right // traverse down // traverse from right to left // traverse up // move boundaries inward }
This method lets you easily and correctly read or print matrix elements in a spiral order, which is useful in many problems and visual patterns.
Think of a robot vacuum cleaning a square room by moving along the walls first, then spiraling inward to cover every spot without missing any area.
Manual spiral reading is confusing and error-prone.
Spiral Matrix Traversal uses boundaries to guide movement.
It ensures all elements are visited once in a neat spiral order.
Practice
Spiral Matrix Traversal?Solution
Step 1: Understand traversal pattern
Spiral traversal means visiting matrix elements starting from the outer layer and moving inward in a circular pattern.Step 2: Identify the traversal goal
The goal is to visit every element exactly once in a spiral order, usually starting from the top-left corner.Final Answer:
To visit all elements of a matrix in a spiral order starting from the top-left corner -> Option AQuick Check:
Spiral traversal = visiting elements in spiral order [OK]
- Confusing spiral traversal with sorting
- Thinking it only visits diagonal elements
- Assuming it transposes the matrix
matrix?Solution
Step 1: Understand matrix dimensions in C#
matrix.GetLength(0) gives number of rows, matrix.GetLength(1) gives number of columns.Step 2: Set boundaries correctly
Top and left start at 0; bottom and right are last indices (length - 1) for rows and columns respectively.Final Answer:
int top = 0, bottom = matrix.GetLength(0) - 1, left = 0, right = matrix.GetLength(1) - 1; -> Option DQuick Check:
Correct boundary initialization = int top = 0, bottom = matrix.GetLength(0) - 1, left = 0, right = matrix.GetLength(1) - 1; [OK]
- Using matrix.Length which is total elements, not rows or columns
- Swapping rows and columns in GetLength calls
- Starting boundaries at 1 instead of 0
int[,] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
List<int> result = new List<int>();
int top = 0, bottom = 2, left = 0, right = 2;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) result.Add(matrix[top, i]);
top++;
for (int i = top; i <= bottom; i++) result.Add(matrix[i, right]);
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) result.Add(matrix[bottom, i]);
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) result.Add(matrix[i, left]);
left++;
}
}
Console.WriteLine(string.Join(", ", result));Solution
Step 1: Traverse top row left to right
Elements added: 1, 2, 3; then top moves from 0 to 1.Step 2: Traverse right column top to bottom
Elements added: 6, 9; right moves from 2 to 1.Step 3: Traverse bottom row right to left
Elements added: 8, 7; bottom moves from 2 to 1.Step 4: Traverse left column bottom to top
Elements added: 4; left moves from 0 to 1.Step 5: Repeat for inner layer
Now top=1, bottom=1, left=1, right=1; add element 5.Final Answer:
1, 2, 3, 6, 9, 8, 7, 4, 5 -> Option AQuick Check:
Spiral order of 3x3 matrix = 1, 2, 3, 6, 9, 8, 7, 4, 5 [OK]
- Not updating boundaries correctly after each side
- Skipping the inner elements
- Reversing traversal directions incorrectly
int top = 0, bottom = matrix.GetLength(0), left = 0, right = matrix.GetLength(1) - 1;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++) Console.Write(matrix[top, i] + " ");
top++;
for (int i = top; i <= bottom; i++) Console.Write(matrix[i, right] + " ");
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) Console.Write(matrix[bottom, i] + " ");
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) Console.Write(matrix[i, left] + " ");
left++;
}
}Solution
Step 1: Check boundary initialization
bottom is set to matrix.GetLength(0), which is out of index range since indexing is zero-based.Step 2: Correct bottom boundary
bottom should be matrix.GetLength(0) - 1 to point to the last valid row index.Final Answer:
Change bottom to matrix.GetLength(0) - 1 to avoid index out of range -> Option BQuick Check:
Zero-based indexing means last index = length - 1 [OK]
- Using length instead of length - 1 for last index
- Removing boundary checks causing infinite loops
- Starting indices at 1 instead of 0
int[,] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};What is the spiral traversal output?
Solution
Step 1: Traverse top row left to right
Elements: 1, 2, 3, 4; top moves from 0 to 1.Step 2: Traverse right column top to bottom
Elements: 8, 12; right moves from 3 to 2.Step 3: Traverse bottom row right to left
Elements: 11, 10, 9; bottom moves from 2 to 1.Step 4: Traverse left column bottom to top
Elements: 5; left moves from 0 to 1.Step 5: Traverse inner row left to right
Elements: 6, 7; top moves from 1 to 2.Final Answer:
1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7 -> Option CQuick Check:
Spiral order of 3x4 matrix = 1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7 [OK]
- Mixing order of inner elements
- Skipping last inner row or column
- Confusing left and right boundaries
Ready to prove it?
Pass the test and this pattern moves unknown
