0
0
NumPydata~20 mins

Negative indexing in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Negative Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A30
B20
C40
D50
Attempts:
2 left
💡 Hint
Negative index -1 refers to the last element, -2 the second last, and so on.
data_output
intermediate
2: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)
A
[[2 3]
 [5 6]]
B
[[4 5]
 [7 8]]
C
[[6]
 [9]]
D
[[5 6]
 [8 9]]
Attempts:
2 left
💡 Hint
Negative slicing starts counting from the end of each dimension.
🔧 Debug
advanced
2: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])
ATypeError: negative index not supported
BIndexError: index -4 is out of bounds for axis 0 with size 3
CValueError: invalid negative index
DNo error, output is 1
Attempts:
2 left
💡 Hint
Negative indices must be within the array length when counted from the end.
🚀 Application
advanced
2:00remaining
Using negative indexing to reverse a numpy array
Which code snippet correctly reverses a numpy array using negative indexing?
Aarr[::-1]
Barr[-1::-1]
Carr[-1:-len(arr):-1]
Darr[-1:0:-1]
Attempts:
2 left
💡 Hint
Slicing with step -1 reverses the array.
🧠 Conceptual
expert
3: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]?
A(4, 3, 5)
B(4, 2, 4)
C(4, 3, 4)
D(4, 2, 5)
Attempts:
2 left
💡 Hint
Negative slicing counts from the end, and the slice excludes the last 2 elements in the last dimension.