Challenge - 5 Problems
Negative Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of negative indexing on 1D numpy array
What is the output of this code snippet using negative indexing on a numpy array?
NumPy
import numpy as np arr = np.array([10, 20, 30, 40, 50]) result = arr[-3] print(result)
Attempts:
2 left
💡 Hint
Negative index -1 refers to the last element, -2 the second last, and so on.
✗ Incorrect
Negative indexing counts from the end of the array. Here, -3 means the third element from the end, which is 30.
❓ data_output
intermediate2:00remaining
Slice output using negative indices on 2D numpy array
What is the output of slicing this 2D numpy array using negative indices?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) slice_result = arr[-2:, -2:] print(slice_result)
Attempts:
2 left
💡 Hint
Negative slicing starts counting from the end of each dimension.
✗ Incorrect
The slice arr[-2:, -2:] selects the last two rows and last two columns, resulting in [[5,6],[8,9]].
🔧 Debug
advanced2:00remaining
Identify the error in negative indexing with numpy array
What error will this code raise when using negative indexing?
NumPy
import numpy as np arr = np.array([1, 2, 3]) print(arr[-4])
Attempts:
2 left
💡 Hint
Negative indices must be within the array length when counted from the end.
✗ Incorrect
Index -4 is out of bounds because the array length is 3, so valid negative indices are -1, -2, -3 only.
🚀 Application
advanced2:00remaining
Using negative indexing to reverse a numpy array
Which code snippet correctly reverses a numpy array using negative indexing?
Attempts:
2 left
💡 Hint
Slicing with step -1 reverses the array.
✗ Incorrect
arr[::-1] returns the array reversed. Other options either miss elements or cause empty slices.
🧠 Conceptual
expert3:00remaining
Effect of negative indexing on multidimensional numpy array shape
Given a 3D numpy array with shape (4, 5, 6), what is the shape of the result after slicing with arr[:, -3:, :-2]?
Attempts:
2 left
💡 Hint
Negative slicing counts from the end, and the slice excludes the last 2 elements in the last dimension.
✗ Incorrect
arr[:, -3:, :-2] selects all in first dim (4), last 3 in second dim (5-3=2 to 4), and all except last 2 in third dim (6-2=4). So shape is (4,3,4).