Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does it mean when a NumPy array is contiguous?
easy
A. It has more than one dimension.
B. It contains only positive numbers.
C. Its data is stored in one continuous block of memory.
D. It is a copy of another array.

Solution

  1. Step 1: Understand contiguous memory

    A contiguous array stores all its elements in one continuous block of memory, which helps with fast access and efficient computation.
  2. Step 2: Compare with other options

    Options B, C, and D describe other properties unrelated to memory layout.
  3. Final Answer:

    Its data is stored in one continuous block of memory. -> Option C
  4. Quick Check:

    Contiguous = continuous memory block [OK]
Hint: Contiguous means data is stored without gaps [OK]
Common Mistakes:
  • Confusing contiguous with array shape
  • Thinking contiguous means sorted data
  • Assuming contiguous means a copy
2. Which of the following is the correct way to create a view using NumPy stride tricks?
easy
A. np.array(arr, copy=True)
B. np.copy(arr).reshape(3,3)
C. arr.flatten()
D. np.lib.stride_tricks.as_strided(arr, shape=(3,3), strides=(8,8))

Solution

  1. Step 1: Identify stride tricks usage

    The function np.lib.stride_tricks.as_strided creates a view with a new shape and strides without copying data.
  2. Step 2: Check other options

    Options B, C, and D create copies or flatten arrays, not views with stride tricks.
  3. Final Answer:

    np.lib.stride_tricks.as_strided(arr, shape=(3,3), strides=(8,8)) -> Option D
  4. Quick Check:

    Stride tricks use as_strided() [OK]
Hint: Use as_strided() to create views with custom strides [OK]
Common Mistakes:
  • Using copy() instead of as_strided()
  • Flattening instead of changing strides
  • Confusing reshape with stride tricks
3. What will be the output of this code?
import numpy as np
arr = np.arange(6)
view = np.lib.stride_tricks.as_strided(arr, shape=(3,), strides=(2*arr.itemsize,))
view[0] = 100
print(arr)
medium
A. [100 1 2 3 4 100]
B. [100 1 2 3 4 5]
C. [100 1 100 3 4 5]
D. [0 1 2 3 4 5]

Solution

  1. Step 1: Understand stride and view

    The view selects elements with stride 2*itemsize, so it accesses arr[0], arr[2], arr[4]. Changing view[0] changes arr[0], changing view[1] changes arr[2], etc.
  2. Step 2: Analyze the assignment and print

    view[0] = 100 sets arr[0] = 100. The print shows arr with arr[0] changed, arr[2] and arr[4] unchanged.
  3. Final Answer:

    [100 1 2 3 4 5] -> Option B
  4. Quick Check:

    Stride view modifies original array [OK]
Hint: Stride selects spaced elements; modifying view changes original [OK]
Common Mistakes:
  • Assuming view is a copy, not a view
  • Ignoring stride size effect
  • Expecting all elements to change
4. Identify the error in this code snippet:
import numpy as np
arr = np.arange(4)
view = np.lib.stride_tricks.as_strided(arr, shape=(3,3), strides=(8,8))
print(view)
medium
A. The shape is incompatible with the original array size.
B. The strides are incorrect for the array's data type.
C. as_strided cannot be used on 1D arrays.
D. The code will run without errors and print a 3x3 view.

Solution

  1. Step 1: Check shape vs original array size

    The original array has 4 elements, but the requested view shape is (3,3) = 9 elements, which is larger than original data.
  2. Step 2: Understand stride_tricks constraints

    as_strided creates views without copying, so shape must fit within original data size. Here, it does not, causing invalid memory access.
  3. Final Answer:

    The shape is incompatible with the original array size. -> Option A
  4. Quick Check:

    View shape must fit original array size [OK]
Hint: View shape must not exceed original array size [OK]
Common Mistakes:
  • Ignoring array size vs view shape
  • Assuming as_strided copies data
  • Misunderstanding stride meaning
5. You have a large 1D NumPy array representing sensor data. You want to create a 2D sliding window view of size 4 without copying data. Which approach correctly uses stride tricks to achieve this?
hard
A. Use np.lib.stride_tricks.as_strided with shape=(len(arr)-3, 4) and strides=(arr.itemsize, arr.itemsize).
B. Use arr.reshape(-1, 4) to create windows.
C. Use np.copy(arr).reshape(-1, 4) to avoid modifying original data.
D. Use arr.flatten() and then slice manually.

Solution

  1. Step 1: Understand sliding window with stride tricks

    Sliding window views use as_strided with shape=(number_of_windows, window_size) and strides that move one element at a time for rows and one element for columns.
  2. Step 2: Check options for correct usage

    Use np.lib.stride_tricks.as_strided with shape=(len(arr)-3, 4) and strides=(arr.itemsize, arr.itemsize). correctly sets shape and strides to create overlapping windows without copying. Options B and C reshape but do not create sliding windows. Use arr.flatten() and then slice manually. flattens but does not create windows.
  3. Final Answer:

    Use np.lib.stride_tricks.as_strided with shape=(len(arr)-3, 4) and strides=(arr.itemsize, arr.itemsize). -> Option A
  4. Quick Check:

    Sliding windows need as_strided with correct shape and strides [OK]
Hint: Sliding windows need as_strided with overlapping strides [OK]
Common Mistakes:
  • Using reshape instead of stride tricks
  • Copying data unnecessarily
  • Incorrect stride values causing wrong windows
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard