Array Rotation Techniques in DSA Go - Time & Space Complexity
When we rotate an array, we move elements around to new positions. Understanding how long this takes helps us choose the best method.
We want to know how the time needed changes as the array gets bigger.
Analyze the time complexity of the following code snippet.
func rotateArray(arr []int, k int) []int {
n := len(arr)
k = k % n
rotated := make([]int, n)
for i := 0; i < n; i++ {
rotated[(i+k)%n] = arr[i]
}
return rotated
}
This code rotates an array to the right by k steps by creating a new array and placing elements at their new positions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop that goes through each element of the array once.
- How many times: Exactly n times, where n is the size of the array.
As the array size grows, the number of steps grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: Doubling the array size doubles the work needed.
Time Complexity: O(n)
This means the time to rotate grows in direct proportion to the array size.
[X] Wrong: "Rotating an array by k steps takes constant time because we only move k elements."
[OK] Correct: Even though k might be small, we must reposition every element to build the rotated array, so the work depends on the whole array size.
Knowing how array rotation scales helps you explain your approach clearly and choose efficient methods in real coding tasks.
"What if we rotated the array in place without using extra space? How would the time complexity change?"
Practice
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 CQuick Check:
Left rotate 1 = first element to end [OK]
- Confusing left rotation with right rotation
- Thinking rotation sorts the array
- Assuming rotation reverses the array
arr to the right by 1 position?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 DQuick Check:
Right rotate 1 = last element front [OK]
- Using wrong slice indices
- Appending single element without slice expansion
- Confusing left and right rotation 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)Solution
Step 1: Calculate effective rotation
k=2, n=5, so k%5=2 means rotate right by 2.Step 2: Perform rotation
arr[n-k:] = arr[3:] = [40,50], arr[:n-k] = arr[:3] = [10,20,30]. Append gives [40,50,10,20,30].Final Answer:
[40 50 10 20 30] -> Option AQuick Check:
Right rotate 2 = last 2 front [OK]
- Not using modulo for k
- Swapping slice indices incorrectly
- Confusing left and right rotation
arr := []int{1, 2, 3, 4, 5}
k := 3
rotated := append(arr[k:], arr[k:]...)
fmt.Println(rotated)Solution
Step 1: Analyze append arguments
Both slices are arr[k:], so elements from index 3 to end repeated twice.Step 2: Identify missing part
Left rotation requires appending arr[:k] after arr[k:], but code repeats arr[k:] instead.Final Answer:
The slice is appended twice from the same index, missing first part -> Option AQuick Check:
Left rotate needs arr[k:] + arr[:k] [OK]
- Appending same slice twice
- Ignoring first k elements in rotation
- Not using slice expansion operator
arr = []int{5, 10, 15, 20, 25, 30}, how would you rotate it left by 4 positions using Go slices? Choose the correct code snippet.Solution
Step 1: Understand left rotation by 4
Left rotation by 4 moves first 4 elements to the end.Step 2: Use slicing correctly
arr[4:] gives elements from index 4 to end, arr[:4] gives first 4 elements. Append arr[4:] + arr[:4] rotates left by 4.Final Answer:
rotated := append(arr[4:], arr[:4]...) -> Option BQuick Check:
Left rotate 4 = arr[4:] + arr[:4] [OK]
- Swapping slice order
- Using wrong slice indices
- Confusing left and right rotation
Ready to prove it?
Pass the test and this pattern moves unknown
