0
0
NumPydata~20 mins

Strides and how data is accessed in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stride Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A(12, 4)
B(8, 4)
C(24, 8)
D(4, 12)
Attempts:
2 left
💡 Hint
Remember that strides show the number of bytes to step in each dimension.
data_output
intermediate
2: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)
A(4, 12)
B(12, 4)
C(8, 16)
D(16, 8)
Attempts:
2 left
💡 Hint
Transpose swaps the axes and their strides.
🔧 Debug
advanced
2: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)
ABecause numpy does not support stride changes on slicing.
BBecause strides always remain constant regardless of slicing.
CBecause the stride is the number of bytes to step, and it is multiplied by the step size in slicing.
DBecause slicing with step creates a view with the same stride multiplied by the step size.
Attempts:
2 left
💡 Hint
Think about how stride relates to step size in slicing.
🚀 Application
advanced
2: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?
A4 bytes
B8 bytes
C16 bytes
D32 bytes
Attempts:
2 left
💡 Hint
Stride for last dimension equals dtype size in bytes.
🧠 Conceptual
expert
3:00remaining
How does numpy use strides to access elements efficiently?
Which statement best explains how numpy uses strides to access elements in memory?
AStrides define the total size of the array in bytes and are unrelated to element access.
BStrides are used to copy data into new arrays for each operation to avoid modifying the original data.
CStrides are only used when arrays are contiguous and ignored otherwise.
DStrides tell numpy how many bytes to jump to move to the next element along each axis, enabling fast memory access without copying data.
Attempts:
2 left
💡 Hint
Think about how numpy avoids copying data when slicing or reshaping.