Array rotation means moving elements of an array to the left or right by a certain number of positions, wrapping around the ends.
Click to reveal answer
beginner
Explain left rotation of an array by 1 position.
Left rotation by 1 moves each element one step to the left. The first element moves to the end of the array.
Click to reveal answer
intermediate
How does the reversal algorithm help in array rotation?
The reversal algorithm rotates an array by reversing parts of the array in steps, then reversing the whole array to achieve rotation efficiently.
Click to reveal answer
intermediate
What is the time complexity of rotating an array using the reversal algorithm?
The time complexity is O(n), where n is the number of elements in the array, because each element is reversed at most twice.
Click to reveal answer
beginner
Describe the difference between left rotation and right rotation.
Left rotation moves elements towards the start of the array, wrapping the first elements to the end. Right rotation moves elements towards the end, wrapping the last elements to the front.
Click to reveal answer
What happens to the first element in a left rotation by 1?
AIt moves to the second position
BIt stays in the same place
CIt moves to the end of the array
DIt is removed from the array
✗ Incorrect
In left rotation by 1, the first element moves to the end of the array.
Which algorithm rotates an array by reversing parts of it?
AReversal algorithm
BBubble sort
CSelection sort
DInsertion sort
✗ Incorrect
The reversal algorithm rotates an array by reversing segments and then the whole array.
What is the time complexity of rotating an array using the reversal algorithm?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
Reversal algorithm runs in linear time O(n) because it reverses parts of the array a fixed number of times.
In right rotation by 1, where does the last element go?
AIt stays at the end
BIt moves to the front of the array
CIt moves to the middle
DIt is removed
✗ Incorrect
In right rotation by 1, the last element moves to the front of the array.
If you rotate an array of length n by n positions, what is the result?
AArray is reversed
BArray is empty
CArray is sorted
DArray remains the same
✗ Incorrect
Rotating by n positions (the array length) results in the same array because elements wrap around fully.
Explain how to perform a left rotation of an array by d positions using the reversal algorithm.
Think about reversing parts and then the entire array.
You got /3 concepts.
Describe the difference between left and right rotations with examples.
Consider what happens to the first and last elements.
You got /3 concepts.
Practice
(1/5)
1. What does rotating an array to the right by 1 position mean? arr = [1, 2, 3, 4] rotated right by 1 becomes?
easy
A. [4, 1, 2, 3]
B. [2, 3, 4, 1]
C. [1, 2, 3, 4]
D. [3, 4, 1, 2]
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 A
Quick Check:
Right rotate by 1 = last element front [OK]
Hint: Last element moves front on right rotation by 1 [OK]
Common Mistakes:
Confusing left and right rotation
Shifting elements left instead of right
Not moving last element to front
2. Which of the following is the correct JavaScript syntax to rotate an array arr to the left by 1 position?
easy
A. arr.unshift(arr.pop())
B. arr.push(arr.shift())
C. arr.pop(arr.unshift())
D. arr.shift(arr.push())
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 B
Quick Check:
Left rotate = shift then push [OK]
Hint: Left rotate = shift first, then push it back [OK]