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 following Java code after rotating the array left by 2 positions?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; 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]; } for (int val : rotated) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Think about how the index shifts when rotating left by 2.
✗ Incorrect
Rotating left by 2 means elements move 2 positions to the left. The element at index 0 moves to index n-2, and so on. The code uses modulo to wrap around.
❓ Predict Output
intermediateOutput of Right Rotation by 3
What is the output of the following Java code after rotating the array right by 3 positions?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6}; 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]; } for (int val : rotated) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Right rotation moves elements to the right by d positions.
✗ Incorrect
Each element moves d positions forward. Using modulo ensures wrapping around the array.
🧠 Conceptual
advancedTime Complexity of Rotation by Reversal Algorithm
What is the time complexity of rotating an array of size n by d positions using the reversal algorithm?
Attempts:
2 left
💡 Hint
The reversal algorithm involves reversing parts of the array multiple times.
✗ Incorrect
The reversal algorithm reverses the first d elements, then the rest, then the whole array. Each reversal is O(k) where k is the length reversed. Total is O(n).
🔧 Debug
advancedIdentify the Bug in Rotation Code
What error will the following Java code produce when rotating an array left by d positions?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {7, 8, 9, 10}; int d = 5; int n = arr.length; int[] rotated = new int[n]; for (int i = 0; i < n; i++) { rotated[i] = arr[(i + d)]; } for (int val : rotated) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Check how the index is calculated inside the loop.
✗ Incorrect
The code accesses arr[i + d] without modulo, so when i + d >= n, it causes ArrayIndexOutOfBoundsException.
🚀 Application
expertMinimum Rotations to Match Arrays
Given two arrays A and B of the same size n, what is the minimum number of left rotations needed on A to make it equal to B? If not possible, return -1. Which approach is best to solve this efficiently?
Attempts:
2 left
💡 Hint
Think about how rotations relate to substrings in concatenated arrays.
✗ Incorrect
Concatenating A with itself contains all rotations of A as substrings. Checking if B is substring gives rotation count efficiently in O(n).
