Challenge - 5 Problems
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Rotation by 2 Positions
What is the output of the following Go code that rotates an array to the left by 2 positions?
DSA Go
package main import "fmt" func main() { arr := []int{10, 20, 30, 40, 50} n := 2 rotated := append(arr[n:], arr[:n]...) fmt.Println(rotated) }
Attempts:
2 left
💡 Hint
Think about moving the first n elements to the end of the array.
✗ Incorrect
The code slices the array from index 2 to the end, then appends the first 2 elements at the end, effectively rotating left by 2.
❓ Predict Output
intermediateOutput of Right Rotation by 3 Positions
What is the output of this Go code that rotates an array to the right by 3 positions?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3, 4, 5, 6} n := 3 length := len(arr) rotated := append(arr[length-n:], arr[:length-n]...) fmt.Println(rotated) }
Attempts:
2 left
💡 Hint
Right rotation by n means last n elements come to front.
✗ Incorrect
The code takes the last 3 elements and places them at the front, then appends the rest.
🔧 Debug
advancedIdentify the Error in Rotation Code
What error will this Go code produce when trying to rotate an array left by 4 positions?
DSA Go
package main import "fmt" func main() { arr := []int{7, 8, 9} n := 4 rotated := append(arr[n:], arr[:n]...) fmt.Println(rotated) }
Attempts:
2 left
💡 Hint
Check if n is larger than the array length.
✗ Incorrect
The code tries to slice beyond the array length because n=4 but array length is 3, causing a runtime panic.
🧠 Conceptual
advancedMinimum Rotations to Restore Original Array
Given an array of length 6, if you rotate it left by 2 positions three times in a row, what will be the final array compared to the original?
Attempts:
2 left
💡 Hint
Think about total rotation after 3 times 2 positions.
✗ Incorrect
Rotating left by 2 positions three times equals rotating left by 6 positions, which is the array length, so the array returns to original.
🚀 Application
expertOutput After Multiple Mixed Rotations
What is the output of this Go code after performing a left rotation by 1, then a right rotation by 2 on the array?
DSA Go
package main import "fmt" func main() { arr := []int{100, 200, 300, 400, 500} // Left rotate by 1 n1 := 1 arr = append(arr[n1:], arr[:n1]...) // Right rotate by 2 n2 := 2 length := len(arr) arr = append(arr[length-n2:], arr[:length-n2]...) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Perform each rotation step carefully and track the array state.
✗ Incorrect
After left rotation by 1, array is [200 300 400 500 100]. Then right rotation by 2 moves last two elements to front, resulting in [400 500 200 300 100].
