0
0
NumPydata~20 mins

Contiguous arrays and stride tricks in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stride Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array flags after slicing
What is the output of the following code snippet regarding the contiguity flags of the sliced array?
NumPy
import numpy as np
arr = np.arange(10)
sliced = arr[2:8]
print(sliced.flags['C_CONTIGUOUS'], sliced.flags['F_CONTIGUOUS'])
ATrue True
BFalse True
CTrue False
DFalse False
Attempts:
2 left
💡 Hint
Slicing a 1D numpy array usually returns a view that is still contiguous in memory.
data_output
intermediate
2:00remaining
Shape and strides of a reshaped array
Given the code below, what are the shape and strides of the reshaped array?
NumPy
import numpy as np
arr = np.arange(12)
reshaped = arr.reshape((3,4))
print(reshaped.shape, reshaped.strides)
A(3, 4) (32, 8)
B(3, 4) (8, 32)
C(4, 3) (32, 8)
D(4, 3) (8, 32)
Attempts:
2 left
💡 Hint
Each element is 8 bytes (int64), and strides indicate bytes to step in each dimension.
🔧 Debug
advanced
2:30remaining
Why does this stride trick code raise an error?
Consider this code using numpy stride tricks. Why does it raise an error?
NumPy
import numpy as np
from numpy.lib.stride_tricks import as_strided
arr = np.arange(5)
window = as_strided(arr, shape=(3,3), strides=(8,8))
print(window)
AIt raises a TypeError because strides must be integers, but 8 is a float.
BIt raises a ValueError because the requested shape and strides exceed the original array's memory.
CIt raises an IndexError because the shape is larger than the array length.
DIt raises no error and prints a 3x3 window.
Attempts:
2 left
💡 Hint
Check if the strides and shape fit inside the original array's memory buffer.
visualization
advanced
2:30remaining
Visualizing memory layout with strides
Which option correctly describes the memory layout visualization of a 2D numpy array with shape (2,3) and strides (24,8)?
NumPy
import numpy as np
arr = np.array([[1,2,3],[4,5,6]], dtype=np.int64)
print(arr.strides)
AEach element is 24 bytes apart; array is 1D in memory.
BEach row is 8 bytes apart, each column 24 bytes apart; elements are stored column-wise.
CEach row is 24 bytes apart, each column 8 bytes apart; elements are stored row-wise.
DStrides indicate the number of elements, not bytes, between rows and columns.
Attempts:
2 left
💡 Hint
Stride values are in bytes, and int64 elements are 8 bytes each.
🚀 Application
expert
3:00remaining
Using stride tricks to create sliding windows
Which option correctly creates a sliding window view of size 3 over a 1D numpy array using stride tricks?
NumPy
import numpy as np
from numpy.lib.stride_tricks import as_strided
arr = np.arange(5)
Aas_strided(arr, shape=(3,), strides=(8,))
Bas_strided(arr, shape=(3, 3), strides=(8, 8))
Cas_strided(arr, shape=(3, 3), strides=(8, 24))
Das_strided(arr, shape=(3, 3), strides=(8, 8))[:,:]
Attempts:
2 left
💡 Hint
Sliding window of size 3 over 1D array should have shape (3, window_size) with correct strides.