Challenge - 5 Problems
Stride Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Understanding numpy array strides
What is the output of the following code showing the strides of a numpy array?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32) print(arr.strides)
Attempts:
2 left
💡 Hint
Remember that strides show the number of bytes to step in each dimension.
✗ Incorrect
The array has shape (2,3) with dtype int32 (4 bytes). The stride for the first dimension is 3*4=12 bytes, for the second dimension is 4 bytes.
❓ data_output
intermediate2:00remaining
Effect of transpose on strides
What are the strides of the array after transposing it?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32) arr_t = arr.T print(arr_t.strides)
Attempts:
2 left
💡 Hint
Transpose swaps the axes and their strides.
✗ Incorrect
Transposing swaps the shape and strides. Original strides (12,4) become (4,12).
🔧 Debug
advanced2:00remaining
Why does this slicing not change strides as expected?
Consider this code:
import numpy as np
arr = np.arange(10)
sliced = arr[::2]
print(sliced.strides)
Why does the stride value double compared to the original array?
NumPy
import numpy as np arr = np.arange(10) sliced = arr[::2] print(sliced.strides)
Attempts:
2 left
💡 Hint
Think about how stride relates to step size in slicing.
✗ Incorrect
The stride reflects the number of bytes to jump to get the next element. When slicing with step 2, the stride doubles in bytes.
🚀 Application
advanced2:00remaining
Predicting memory layout from strides
Given a numpy array with shape (3, 4) and strides (32, 8), what is the dtype size in bytes?
Attempts:
2 left
💡 Hint
Stride for last dimension equals dtype size in bytes.
✗ Incorrect
The stride for the last dimension is the size of one element in bytes. Here it is 8, so dtype size is 8 bytes.
🧠 Conceptual
expert3:00remaining
How does numpy use strides to access elements efficiently?
Which statement best explains how numpy uses strides to access elements in memory?
Attempts:
2 left
💡 Hint
Think about how numpy avoids copying data when slicing or reshaping.
✗ Incorrect
Numpy uses strides to calculate the memory offset for each element, allowing views and efficient access without copying.