0
0
DSA Pythonprogramming~20 mins

Array Rotation Techniques in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[30, 40, 50, 10, 20]
B[40, 50, 10, 20, 30]
C[50, 10, 20, 30, 40]
D[20, 30, 40, 50, 10]
Attempts:
2 left
💡 Hint
Think about how slicing works for left rotation.
Predict Output
intermediate
2: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)
A[5, 6, 1, 2, 3, 4]
B[4, 5, 6, 1, 2, 3]
C[3, 4, 5, 6, 1, 2]
D[6, 1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Right rotation means taking last elements to front.
🔧 Debug
advanced
2: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))
ASyntaxError: invalid syntax
BIndexError: list index out of range
CTypeError: can only concatenate list (not "int") to list
DNo error, output: [3, 4, 1, 2]
Attempts:
2 left
💡 Hint
Look at what arr[n] returns and what is being concatenated.
Predict Output
advanced
2: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)
A[7, 8, 9]
B[]
C[8, 9, 7]
D[9, 7, 8]
Attempts:
2 left
💡 Hint
Rotating by array length should return the original array.
🧠 Conceptual
expert
2: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?
A0
B1
C3
D6
Attempts:
2 left
💡 Hint
Think about how many rotations bring the array back to start.