Challenge - 5 Problems
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Left Rotation by 2
What is the output of the array after performing a left rotation by 2 positions?
DSA C
int arr[] = {10, 20, 30, 40, 50}; int n = 5, d = 2; // Left rotate by d int rotated[5]; for(int i = 0; i < n; i++) { rotated[i] = arr[(i + d) % n]; } for(int i = 0; i < n; i++) { printf("%d ", rotated[i]); }
Attempts:
2 left
💡 Hint
Think about moving the first two elements to the end.
✗ Incorrect
Left rotation by 2 moves elements at index 0 and 1 to the end, so the array starts from index 2.
❓ Predict Output
intermediate2:00remaining
Output of Right Rotation by 3
What is the output of the array after performing a right rotation by 3 positions?
DSA C
int arr[] = {1, 2, 3, 4, 5, 6}; int n = 6, d = 3; int rotated[6]; for(int i = 0; i < n; i++) { rotated[(i + d) % n] = arr[i]; } for(int i = 0; i < n; i++) { printf("%d ", rotated[i]); }
Attempts:
2 left
💡 Hint
Right rotation by 3 moves last 3 elements to the front.
✗ Incorrect
Right rotation by 3 shifts elements so that the last 3 elements come to the front.
🔧 Debug
advanced2:00remaining
Identify the Error in Rotation Code
What error will this code produce when trying to left rotate an array by d positions?
DSA C
int arr[] = {7, 8, 9, 10}; int n = 4, d = 5; int rotated[4]; for(int i = 0; i < n; i++) { rotated[i] = arr[(i + d) % n]; } for(int i = 0; i < n; i++) { printf("%d ", rotated[i]); }
Attempts:
2 left
💡 Hint
Consider how modulo operator handles d greater than n.
✗ Incorrect
Modulo operator ensures index wraps correctly even if d > n, so no error occurs.
❓ Predict Output
advanced2:00remaining
Output of Reversal Algorithm for Left Rotation
What is the output of the array after left rotating by 3 using the reversal algorithm?
DSA C
void reverse(int arr[], int start, int end) { while(start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } int arr[] = {1, 2, 3, 4, 5, 6, 7}; int n = 7, d = 3; reverse(arr, 0, d - 1); reverse(arr, d, n - 1); reverse(arr, 0, n - 1); for(int i = 0; i < n; i++) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
Reversal algorithm reverses parts then whole array.
✗ Incorrect
First reverse first 3 elements, then last 4, then whole array to get left rotation by 3.
🧠 Conceptual
expert2:00remaining
Minimum Number of Rotations to Restore Original Array
Given an array of length n, what is the minimum number of left rotations needed to restore the array to its original order?
Attempts:
2 left
💡 Hint
Think about how many times you must rotate to get back to start.
✗ Incorrect
Rotating n times brings every element back to its original position.
