Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reversing an array using slicing
What is the output of the following Python code that reverses an array using slicing?
DSA Python
arr = [10, 20, 30, 40, 50] reversed_arr = arr[::-1] print(reversed_arr)
Attempts:
2 left
💡 Hint
Remember that slicing with [::-1] reverses the list.
✗ Incorrect
Using arr[::-1] creates a new list with elements in reverse order. So the output is the original list reversed.
❓ Predict Output
intermediate2:00remaining
Output after reversing array in-place with two pointers
What is the output of this code that reverses an array in-place using two pointers?
DSA Python
arr = [1, 2, 3, 4, 5] left, right = 0, len(arr) - 1 while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 print(arr)
Attempts:
2 left
💡 Hint
Swapping elements from both ends moves the array towards reversed order.
✗ Incorrect
The two-pointer approach swaps elements from the start and end moving inward, reversing the array in-place.
🔧 Debug
advanced2:00remaining
Identify the error in this array reversal function
What error does this code produce when trying to reverse an array?
DSA Python
def reverse_array(arr): for i in range(len(arr) // 2): arr[i], arr[len(arr) - i - 1] = arr[len(arr) - i - 1], arr[i] arr = [1, 2, 3, 4] reverse_array(arr) print(arr)
Attempts:
2 left
💡 Hint
Check the index used for swapping elements.
✗ Incorrect
The code uses len(arr) - i which is out of range because list indices go from 0 to len(arr)-1.
🧠 Conceptual
advanced1:30remaining
Number of swaps needed to reverse an array of length n
How many element swaps are needed to reverse an array of length n using the two-pointer method?
Attempts:
2 left
💡 Hint
Each swap fixes two elements at once.
✗ Incorrect
Each swap exchanges two elements, so only half the array length swaps are needed.
🚀 Application
expert2:30remaining
Output after reversing a nested array with slicing
What is the output of this code that reverses a nested array using slicing?
DSA Python
arr = [[1, 2], [3, 4], [5, 6]] reversed_arr = arr[::-1] print(reversed_arr)
Attempts:
2 left
💡 Hint
Slicing reverses the outer list but does not reverse inner lists.
✗ Incorrect
The slicing reverses the order of the inner lists but does not change their contents.