Challenge - 5 Problems
Memory Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of array flattening with different memory orders
What is the output of the following code?
import numpy as np arr = np.array([[1, 2], [3, 4]]) flat_c = arr.flatten(order='C') flat_f = arr.flatten(order='F') print(flat_c) print(flat_f)
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) flat_c = arr.flatten(order='C') flat_f = arr.flatten(order='F') print(flat_c) print(flat_f)
Attempts:
2 left
💡 Hint
Remember that C-order flattens row-wise, Fortran-order flattens column-wise.
✗ Incorrect
In C-order, flattening reads rows first: [1, 2, 3, 4]. In Fortran-order, flattening reads columns first: [1, 3, 2, 4].
❓ data_output
intermediate2:00remaining
Shape and strides of arrays with different memory orders
Given the code below, what are the strides of the arrays arr_c and arr_f?
import numpy as np arr_c = np.array([[1, 2], [3, 4]], order='C') arr_f = np.array([[1, 2], [3, 4]], order='F') print(arr_c.strides) print(arr_f.strides)
NumPy
import numpy as np arr_c = np.array([[1, 2], [3, 4]], order='C') arr_f = np.array([[1, 2], [3, 4]], order='F') print(arr_c.strides) print(arr_f.strides)
Attempts:
2 left
💡 Hint
Strides show how many bytes to jump to get to the next element in each dimension.
✗ Incorrect
In C-order, the last axis changes fastest, so strides are (row_stride, col_stride) = (16, 8) for int64 (8 bytes per element). In Fortran-order, the first axis changes fastest, so strides are (8, 16).
🔧 Debug
advanced2:00remaining
Why does this reshape cause an error with Fortran order?
Consider the code below. Which option correctly explains why the reshape fails?
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]], order='F') arr_reshaped = arr.reshape((3, 2), order='C')
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]], order='F') arr_reshaped = arr.reshape((3, 2), order='C')
Attempts:
2 left
💡 Hint
Reshape with order='C' expects the array to be C-contiguous.
✗ Incorrect
The original array is Fortran-contiguous, but reshape with order='C' requires C-contiguous memory. This mismatch causes a ValueError.
🚀 Application
advanced2:00remaining
Choosing memory layout for performance in matrix multiplication
You want to multiply two large matrices using NumPy. Which memory layout choice is generally better for performance and why?
Attempts:
2 left
💡 Hint
Think about how NumPy stores arrays by default and how BLAS libraries expect data.
✗ Incorrect
NumPy stores arrays in C-order by default, and its underlying BLAS libraries are optimized for row-major contiguous data, so using C-order arrays usually gives better performance.
🧠 Conceptual
expert2:00remaining
Effect of memory layout on slicing and views
Which statement about slicing a Fortran-order NumPy array is true?
Attempts:
2 left
💡 Hint
Consider how Fortran-order arrays store data column-wise and how slicing affects contiguity.
✗ Incorrect
Fortran-order arrays store data column-wise, so slicing along the first axis (rows) preserves Fortran-contiguous views. Slicing along other axes may break contiguity or return non-contiguous views.