Challenge - 5 Problems
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Rotation by 2
What is the output of the array after rotating it left by 2 positions?
DSA C#
int[] arr = {1, 2, 3, 4, 5}; int d = 2; int n = arr.Length; int[] rotated = new int[n]; for (int i = 0; i < n; i++) { rotated[i] = arr[(i + d) % n]; } Console.WriteLine(string.Join(", ", rotated));
Attempts:
2 left
💡 Hint
Think about how the index shifts when rotating left by 2.
✗ Incorrect
Rotating left by 2 means the element at index 2 moves to index 0, index 3 to 1, and so on.
❓ Predict Output
intermediateOutput of Right Rotation by 3
What is the output of the array after rotating it right by 3 positions?
DSA C#
int[] arr = {10, 20, 30, 40, 50}; int d = 3; int n = arr.Length; int[] rotated = new int[n]; for (int i = 0; i < n; i++) { rotated[(i + d) % n] = arr[i]; } Console.WriteLine(string.Join(", ", rotated));
Attempts:
2 left
💡 Hint
Right rotation moves elements to higher indexes wrapping around.
✗ Incorrect
Each element moves 3 positions to the right. Using modulo ensures wrapping.
🔧 Debug
advancedIdentify the Error in Rotation Code
What error does this code produce when rotating an array left by d positions?
DSA C#
int[] arr = {1, 2, 3, 4, 5}; int d = 7; int n = arr.Length; int[] rotated = new int[n]; for (int i = 0; i < n; i++) { rotated[i] = arr[((i - d) % n + n) % n]; } Console.WriteLine(string.Join(", ", rotated));
Attempts:
2 left
💡 Hint
Check how modulo behaves with negative numbers in C#.
✗ Incorrect
In C#, modulo with negative numbers can produce negative results, causing invalid array indexing.
🧠 Conceptual
advancedMinimum Rotations to Restore Original Array
Given an array of length 6, how many left rotations are needed to restore the array to its original order?
Attempts:
2 left
💡 Hint
Think about rotating the array fully around.
✗ Incorrect
Rotating the array by its length returns it to the original order.
🚀 Application
expertResult of Multiple Rotations
What is the final array after performing these operations on [1, 2, 3, 4, 5]: rotate left by 2, then rotate right by 3?
DSA C#
int[] arr = {1, 2, 3, 4, 5}; int n = arr.Length; int d1 = 2; // left rotation int d2 = 3; // right rotation int[] temp = new int[n]; for (int i = 0; i < n; i++) { temp[i] = arr[(i + d1) % n]; } int[] result = new int[n]; for (int i = 0; i < n; i++) { result[(i + d2) % n] = temp[i]; } Console.WriteLine(string.Join(", ", result));
Attempts:
2 left
💡 Hint
Combine the rotations carefully using modulo arithmetic.
✗ Incorrect
Left rotation by 2 followed by right rotation by 3 equals a net right rotation of 1 position (3 - 2 = 1). The result is 5, 1, 2, 3, 4.
