📖 Scenario: You are working on a simple program that helps a store manager rotate the order of products on display shelves. This rotation helps to show different products at the front each day.
🎯 Goal: Build a Go program that rotates an array of product IDs to the right by a given number of steps and then prints the rotated array.
📋 What You'll Learn
Create an array called products with the exact values 101, 102, 103, 104, 105
Create a variable called steps and set it to 2
Write code to rotate the products array to the right by steps positions
Print the rotated array in the format: [104 105 101 102 103]
💡 Why This Matters
🌍 Real World
Rotating arrays is useful in scheduling, gaming, and UI where elements need to cycle positions.
💼 Career
Understanding array manipulation is fundamental for software developers working with data transformations and algorithms.
Progress0 / 4 steps
1
Create the initial array of product IDs
Create an array called products with the exact values 101, 102, 103, 104, 105.
DSA Go
Hint
Use a slice of integers with the exact values given.
2
Add the number of rotation steps
Create a variable called steps and set it to 2.
DSA Go
Hint
Use a simple integer variable named steps and assign it the value 2.
3
Rotate the array to the right by the given steps
Write code to rotate the products array to the right by steps positions. Use slicing and concatenation to create the rotated array and store it back in products.
DSA Go
Hint
Use modulo to handle steps larger than array length. Then use slicing and append to rotate.
4
Print the rotated array
Write a fmt.Println statement to print the rotated products array.
DSA Go
Hint
Use fmt.Println(products) to print the rotated array.
Practice
(1/5)
1. What does rotating an array to the left by 1 position do to the elements?
easy
A. Reverses the entire array
B. Moves the last element to the front and shifts others right by one
C. Moves the first element to the end and shifts others left by one
D. Sorts the array in ascending order
Solution
Step 1: Understand left rotation
Left rotation by 1 means the element at index 0 moves to the end.
Step 2: Effect on array elements
All other elements shift one position to the left.
Final Answer:
Moves the first element to the end and shifts others left by one -> Option C
Quick Check:
Left rotate 1 = first element to end [OK]
Hint: Left rotate by 1 moves front element to back [OK]
Common Mistakes:
Confusing left rotation with right rotation
Thinking rotation sorts the array
Assuming rotation reverses the array
2. Which of the following Go code snippets correctly rotates an array arr to the right by 1 position?
easy
A. arr = append(arr[len(arr)-1:], arr[:len(arr)-1])
B. arr = append(arr[1:], arr[0])
C. arr = append(arr[:len(arr)-1], arr[len(arr)-1])
D. arr = append([]int{arr[len(arr)-1]}, arr[:len(arr)-1]...)
Solution
Step 1: Understand right rotation by 1
Right rotation by 1 moves last element to front.
Step 2: Check code correctness
arr = append([]int{arr[len(arr)-1]}, arr[:len(arr)-1]...) prepends last element to the rest correctly using slicing and append.
Final Answer:
arr = append([]int{arr[len(arr)-1]}, arr[:len(arr)-1]...) -> Option D
Quick Check:
Right rotate 1 = last element front [OK]
Hint: Right rotate by 1 prepends last element [OK]
Common Mistakes:
Using wrong slice indices
Appending single element without slice expansion
Confusing left and right rotation code
3. What is the output of the following Go code?
arr := []int{10, 20, 30, 40, 50}
k := 2
n := len(arr)
k = k % n
rotated := append(arr[n-k:], arr[:n-k]...)
fmt.Println(rotated)