Array rotation means shifting the elements of an array to the left or right by a certain number of positions, wrapping around the elements that go past the end.
Click to reveal answer
beginner
Explain the difference between left rotation and right rotation of an array.
Left rotation moves elements towards the start of the array, pushing the first elements to the end. Right rotation moves elements towards the end, pushing the last elements to the front.
Click to reveal answer
intermediate
What is the time complexity of rotating an array by one position using the naive method?
The naive method shifts elements one by one, so rotating by one position takes O(n) time, where n is the array size.
Click to reveal answer
intermediate
Describe the reversal algorithm for array rotation.
The reversal algorithm rotates an array by reversing parts of the array: first reverse the first part, then reverse the second part, and finally reverse the whole array to get the rotated array.
Click to reveal answer
intermediate
Why is the reversal algorithm more efficient than the naive rotation method?
The reversal algorithm rotates the array in O(n) time with only three reversals, avoiding multiple shifts and reducing the number of operations.
Click to reveal answer
What happens to the elements when you rotate an array to the left by 2 positions?
AThe first two elements move to the end, and the rest shift left
BThe last two elements move to the front, and the rest shift right
CAll elements reverse their order
DThe array remains unchanged
✗ Incorrect
Left rotation by 2 moves the first two elements to the end, shifting the rest left.
Which algorithm rotates an array by reversing parts of it?
ANaive rotation
BReversal algorithm
CBubble sort
DSelection sort
✗ Incorrect
The reversal algorithm rotates an array by reversing segments and then the whole array.
What is the time complexity of the reversal algorithm for rotating an array?
AO(n^2)
BO(n log n)
CO(n)
DO(1)
✗ Incorrect
The reversal algorithm completes rotation in linear time O(n).
If you rotate an array of size 5 to the right by 1, where does the last element go?
AIt moves to the middle
BIt is removed
CIt stays at the last position
DIt moves to the front (index 0)
✗ Incorrect
Right rotation by 1 moves the last element to the front.
Which method is less efficient for large arrays when rotating multiple positions?
ANaive rotation by shifting one element at a time
BReversal algorithm
CUsing extra array to copy elements
DNo rotation
✗ Incorrect
Naive rotation shifts elements one by one, which is inefficient for large arrays.
Explain how to rotate an array to the left by d positions using the reversal algorithm.
Think about reversing parts of the array step by step.
You got /3 concepts.
Describe the difference between naive rotation and the reversal algorithm in terms of steps and efficiency.
Compare how many operations each method uses.
You got /4 concepts.
Practice
(1/5)
1. What does a left rotation by 1 position do to the array [1, 2, 3, 4, 5]?
easy
A. Moves the last element to the front, resulting in [5, 1, 2, 3, 4]
B. Moves the first element to the end, resulting in [2, 3, 4, 5, 1]
C. Reverses the array, resulting in [5, 4, 3, 2, 1]
D. Leaves the array unchanged
Solution
Step 1: Understand left rotation by 1
Left rotation by 1 means the first element moves to the end, and all others shift left by one.
Step 2: Apply rotation to the array
Starting with [1, 2, 3, 4, 5], after rotation it becomes [2, 3, 4, 5, 1].
Final Answer:
Moves the first element to the end, resulting in [2, 3, 4, 5, 1] -> Option B
Quick Check:
Left rotate by 1 shifts left, first element goes last [OK]
Hint: Left rotate by 1 moves first element to end [OK]
Common Mistakes:
Confusing left rotation with right rotation
Thinking rotation reverses the array
Assuming array stays the same
2. Which of the following is the correct C syntax to rotate an array arr of size n to the right by 1 position using a temporary variable temp?
easy
A. temp = arr[n-1]; for (int i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp;
B. temp = arr[0]; for (int i = 0; i < n-1; i++) arr[i] = arr[i+1]; arr[n-1] = temp;
C. temp = arr[n]; for (int i = n; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp;
D. temp = arr[1]; for (int i = 1; i < n; i++) arr[i] = arr[i-1]; arr[0] = temp;
Solution
Step 1: Understand right rotation by 1
Right rotation by 1 moves the last element to the front, shifting others right.
Step 2: Check syntax correctness
temp = arr[n-1]; for (int i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp; saves last element arr[n-1], shifts elements from right to left, then sets arr[0] = temp. This matches right rotation logic.
Final Answer:
temp = arr[n-1]; for (int i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp; -> Option A
Quick Check:
Right rotate saves last, shifts right, places last at front [OK]
Hint: Right rotate saves last element, shifts right, places last front [OK]
Common Mistakes:
Using wrong index for temp (like arr[n])
Loop direction incorrect (should go backward for right rotate)
Shifting elements in wrong direction
3. What is the output of the following C code snippet?
int arr[] = {10, 20, 30, 40, 50};
int n = 5, d = 2;
for (int i = 0; i < d; i++) {
int temp = arr[0];
for (int j = 0; j < n-1; j++) {
arr[j] = arr[j+1];
}
arr[n-1] = temp;
}
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
medium
A. 30 40 50 10 20
B. 40 50 10 20 30
C. 20 30 40 50 10
D. 50 10 20 30 40
Solution
Step 1: Understand the code logic
The code performs left rotation by 1, two times (d=2). Each rotation moves first element to end.