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 after rotating array left by 2 positions
What is the output of the following Python code that rotates an array to the left by 2 positions?
DSA Python
arr = [10, 20, 30, 40, 50] rotations = 2 rotated = arr[rotations:] + arr[:rotations] print(rotated)
Attempts:
2 left
💡 Hint
Think about how slicing works for left rotation.
✗ Incorrect
The code slices the array from index 2 to the end, then adds the first two elements at the end. This results in the array rotated left by 2 positions.
❓ Predict Output
intermediate2:00remaining
Output after rotating array right by 3 positions
What is the output of this Python code that rotates an array to the right by 3 positions?
DSA Python
arr = [1, 2, 3, 4, 5, 6] rotations = 3 rotated = arr[-rotations:] + arr[:-rotations] print(rotated)
Attempts:
2 left
💡 Hint
Right rotation means taking last elements to front.
✗ Incorrect
The code takes the last 3 elements and places them at the front, then adds the rest after.
🔧 Debug
advanced2:00remaining
Identify the error in this array rotation code
What error does this code raise when trying to rotate an array left by n positions?
DSA Python
def rotate_left(arr, n): n = n % len(arr) return arr[n:] + arr[n] print(rotate_left([1, 2, 3, 4], 2))
Attempts:
2 left
💡 Hint
Look at what arr[n] returns and what is being concatenated.
✗ Incorrect
arr[n] returns a single element (an int), but arr[n:] is a list. Adding a list and an int causes a TypeError.
❓ Predict Output
advanced2:00remaining
Output after rotating array right by length of array
What is the output of this code that rotates an array right by its own length?
DSA Python
arr = [7, 8, 9] rotations = len(arr) rotated = arr[-rotations:] + arr[:-rotations] print(rotated)
Attempts:
2 left
💡 Hint
Rotating by array length should return the original array.
✗ Incorrect
Rotating an array by its length results in the same array because the elements cycle back to original positions.
🧠 Conceptual
expert2:00remaining
Minimum rotations to restore original array
Given an array of length 6, what is the minimum number of right rotations needed to restore the array to its original order?
Attempts:
2 left
💡 Hint
Think about how many rotations bring the array back to start.
✗ Incorrect
Rotating right by the array length (6) returns the array to its original order. Smaller rotations do not restore it fully.