Array Rotation Techniques in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to rotate an array changes as the array gets bigger.
How does the number of steps grow when we rotate more elements?
Analyze the time complexity of the following code snippet.
function rotateArray(arr, k) {
const n = arr.length;
k = k % n;
const rotated = arr.slice(n - k).concat(arr.slice(0, n - k));
return rotated;
}
This code rotates an array to the right by k steps by slicing and joining parts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating two slices of the array and concatenating them.
- How many times: Each slice goes through part of the array once, so total traversal is the whole array once.
As the array size grows, the number of elements copied grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 elements copied |
| 100 | About 100 elements copied |
| 1000 | About 1000 elements copied |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to rotate grows directly with the number of elements in the array.
[X] Wrong: "Rotating an array by k steps takes constant time because we only move k elements."
[OK] Correct: Even if k is small, slicing and concatenating still processes the whole array, so time depends on n, not just k.
Understanding how array rotation scales helps you explain and choose the best approach in coding challenges and real projects.
"What if we rotated the array in place using reversal steps instead of slicing? How would the time complexity change?"
Practice
arr = [1, 2, 3, 4] rotated right by 1 becomes?Solution
Step 1: Understand right rotation by 1
Right rotation by 1 means the last element moves to the front, and others shift right.Step 2: Apply to given array
Last element 4 moves to front, rest shift right: [4, 1, 2, 3].Final Answer:
[4, 1, 2, 3] -> Option AQuick Check:
Right rotate by 1 = last element front [OK]
- Confusing left and right rotation
- Shifting elements left instead of right
- Not moving last element to front
arr to the left by 1 position?Solution
Step 1: Understand left rotation by 1
Left rotation by 1 means first element moves to the end.Step 2: Match syntax to operation
arr.shift() removes first element, arr.push() adds it to end. So arr.push(arr.shift()) rotates left by 1.Final Answer:
arr.push(arr.shift()) -> Option BQuick Check:
Left rotate = shift then push [OK]
- Using pop instead of shift for left rotation
- Using unshift incorrectly
- Mixing push and pop order
const arr = [10, 20, 30, 40, 50]; const k = 2; const rotated = arr.slice(-k).concat(arr.slice(0, arr.length - k)); console.log(rotated);
Solution
Step 1: Understand slicing for right rotation by k=2
arr.slice(-2) gets last 2 elements: [40, 50]. arr.slice(0, 3) gets first 3 elements: [10, 20, 30].Step 2: Concatenate slices
Concatenate [40, 50] + [10, 20, 30] = [40, 50, 10, 20, 30].Final Answer:
[40, 50, 10, 20, 30] -> Option AQuick Check:
Right rotate by 2 = last 2 front [OK]
- Using slice(0, k) instead of slice(-k)
- Concatenating in wrong order
- Confusing left and right rotation
arr left by k positions:function rotateLeft(arr, k) {
for(let i = 0; i < k; i++) {
arr.unshift(arr.shift());
}
return arr;
}Solution
Step 1: Analyze loop operation
Inside loop, arr.shift() removes first element correctly for left rotation.Step 2: Check insertion method
arr.unshift() adds element to front, which reverses the shift effect. It should be arr.push() to add at end.Final Answer:
No error, code works correctly -> Option CQuick Check:
Code rotates left by k positions correctly [OK]
- Using unshift instead of push
- Confusing shift and pop
- Incorrect loop range
arr = [1, 2, 3, 4, 5], how can you rotate it left by k = 7 positions efficiently without extra space?Solution
Step 1: Calculate effective rotation
Since k=7 and arr.length=5, effective rotation is k % 5 = 2.Step 2: Use reversal algorithm for in-place rotation
Reverse first 2 elements, reverse last 3 elements, then reverse whole array to rotate left by 2 efficiently without extra space.Final Answer:
Rotate left by k % arr.length = 2 positions using reversal algorithm -> Option DQuick Check:
Use k mod length and reversal for efficient rotation [OK]
- Rotating by k without modulo causes extra rotations
- Using slice creates extra space
- Using nested loops is inefficient
Ready to prove it?
Pass the test and this pattern moves unknown
