📖 Scenario: You are working on a robot that needs to clean a room by moving in a spiral path. The room is represented as a grid of numbers where each number shows the dirt level. The robot must visit each cell in a spiral order starting from the top-left corner.
🎯 Goal: Build a Go program that takes a 3x3 matrix and prints the elements in spiral order.
📋 What You'll Learn
Create a 3x3 matrix with exact values
Use variables to track the boundaries of the matrix
Implement the spiral traversal logic using loops
Print the spiral order as a slice of integers
💡 Why This Matters
🌍 Real World
Spiral traversal is useful in image processing, robot path planning, and data visualization where data needs to be accessed in a spiral pattern.
💼 Career
Understanding matrix traversal algorithms is important for software engineers working with graphics, simulations, or any domain involving 2D data structures.
Progress0 / 4 steps
1
Create the 3x3 matrix
Create a 3x3 matrix called matrix with these exact rows: {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
DSA Go
Hint
Use a slice of slices of int to create the matrix.
2
Set up boundary variables
Add four integer variables called top, bottom, left, and right. Set top to 0, bottom to 2, left to 0, and right to 2
DSA Go
Hint
These variables will help track the edges of the matrix during traversal.
3
Implement spiral traversal logic
Create a slice of int called result to store the spiral order. Use a for loop that runs while left <= right and top <= bottom. Inside the loop, traverse from left to right on the top row, then increment top. Next, traverse from top to bottom on the right column, then decrement right. Then, if top <= bottom, traverse from right to left on the bottom row, then decrement bottom. Finally, if left <= right, traverse from bottom to top on the left column, then increment left. Append each visited element to result
DSA Go
Hint
Use nested loops and carefully update the boundary variables after each side traversal.
4
Print the spiral order
Add a fmt.Println statement to print the result slice
DSA Go
Hint
Use fmt.Println(result) to print the spiral order.
Practice
(1/5)
1. What is the main idea behind Spiral Matrix Traversal?
easy
A. Traverse the matrix in a circular pattern starting from the outer layer moving inward
B. Traverse the matrix row by row from top to bottom
C. Traverse the matrix column by column from left to right
D. Traverse only the diagonal elements of the matrix
Solution
Step 1: Understand traversal pattern
Spiral traversal means moving around the edges of the matrix and gradually moving inward in a circular way.
Step 2: Compare options with spiral pattern
Only Traverse the matrix in a circular pattern starting from the outer layer moving inward describes moving in a circular pattern from outer to inner layers, which matches spiral traversal.
Final Answer:
Traverse the matrix in a circular pattern starting from the outer layer moving inward -> Option A
Quick Check:
Spiral traversal = circular outer to inner [OK]
Hint: Think of peeling an onion layer by layer [OK]
Common Mistakes:
Confusing spiral with row-wise traversal
Thinking spiral means diagonal traversal
Ignoring the inward movement after outer layer
2. Which of the following Go code snippets correctly initializes the boundaries for spiral traversal of a matrix named matrix?
easy
A. top, bottom := 0, len(matrix)-1; left, right := 0, len(matrix[0])-1
B. top, bottom := 1, len(matrix); left, right := 1, len(matrix[0])
C. top, bottom := 0, len(matrix); left, right := 0, len(matrix[0])
D. top, bottom := -1, len(matrix)-1; left, right := -1, len(matrix[0])-1
Solution
Step 1: Identify correct boundary initialization
For spiral traversal, top and left start at 0, bottom and right are last indices (length - 1).
Step 2: Check each option
top, bottom := 0, len(matrix)-1; left, right := 0, len(matrix[0])-1 correctly sets top=0, bottom=len(matrix)-1, left=0, right=len(matrix[0])-1. Others have off-by-one or negative values.
Final Answer:
top, bottom := 0, len(matrix)-1; left, right := 0, len(matrix[0])-1 -> Option A
Quick Check:
Boundaries start at 0 and end at length-1 [OK]
Hint: Remember Go slices are 0-indexed, so last index is length-1 [OK]
Common Mistakes:
Using length instead of length-1 for boundaries
Starting boundaries at 1 instead of 0
Using negative indices incorrectly
3. What is the output of the following Go code snippet for spiral traversal?
matrix := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
result := []int{}
top, bottom := 0, 2
left, right := 0, 2
for top <= bottom && left <= right {
for i := left; i <= right; i++ {
result = append(result, matrix[top][i])
}
top++
for i := top; i <= bottom; i++ {
result = append(result, matrix[i][right])
}
right--
for i := right; i >= left; i-- {
result = append(result, matrix[bottom][i])
}
bottom--
for i := bottom; i >= top; i-- {
result = append(result, matrix[i][left])
}
left++
}
fmt.Println(result)
medium
A. [9 8 7 6 5 4 3 2 1]
B. [1 2 3 4 5 6 7 8 9]
C. [1 4 7 8 9 6 3 2 5]
D. [1 2 3 6 9 8 7 4 5]
Solution
Step 1: Trace the spiral traversal steps
Traverse top row left to right: 1,2,3; then right column top to bottom: 6,9; then bottom row right to left: 8,7; then left column bottom to top: 4; then inner layer top row: 5.
Step 2: Collect elements in order
Order is [1,2,3,6,9,8,7,4,5].
Final Answer:
[1 2 3 6 9 8 7 4 5] -> Option D
Quick Check:
Spiral order matches [1 2 3 6 9 8 7 4 5] [OK]
Hint: Follow edges clockwise, then move inward [OK]
Common Mistakes:
Not updating boundaries correctly after each loop
Repeating elements or missing inner elements
Confusing row and column indices
4. Identify the bug in this Go code snippet for spiral traversal:
top, bottom := 0, len(matrix)
left, right := 0, len(matrix[0])
for top <= bottom && left <= right {
for i := left; i <= right; i++ {
fmt.Print(matrix[top][i], " ")
}
top++
// ... rest of traversal
}
medium
A. Missing initialization of top and left variables
B. Using len(matrix) and len(matrix[0]) instead of len(matrix)-1 and len(matrix[0])-1 for boundaries
C. Incorrect loop condition using <= instead of <
D. Using fmt.Print instead of fmt.Println
Solution
Step 1: Check boundary initialization
len(matrix) gives count, but last index is len(matrix)-1. Using len(matrix) causes out-of-range error.
Step 2: Confirm loop indices
Loop uses i <= right, so right must be last valid index, not length.
Final Answer:
Using len(matrix) and len(matrix[0]) instead of len(matrix)-1 and len(matrix[0])-1 for boundaries -> Option B
Quick Check:
Boundaries must be last index = length-1 [OK]
Hint: Remember last index is length minus one [OK]