0
0
NumPydata~20 mins

Memory layout (C-order vs Fortran-order) in NumPy - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[1 2 3 4]
[1 3 2 4]
B
[1 3 2 4]
[1 2 3 4]
C
[1 2 3 4]
[4 3 2 1]
D
[4 3 2 1]
[1 2 3 4]
Attempts:
2 left
💡 Hint
Remember that C-order flattens row-wise, Fortran-order flattens column-wise.
data_output
intermediate
2: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)
A
(8, 8)
(8, 8)
B
(8, 16)
(16, 8)
C
(16, 16)
(16, 16)
D
(16, 8)
(8, 16)
Attempts:
2 left
💡 Hint
Strides show how many bytes to jump to get to the next element in each dimension.
🔧 Debug
advanced
2: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')
ARuns successfully and returns a reshaped array with shape (3, 2).
BRaises ValueError because the memory layout order of the original array does not match the reshape order.
CRaises TypeError because reshape does not accept the order parameter.
DRaises IndexError because the new shape is incompatible with the original shape.
Attempts:
2 left
💡 Hint
Reshape with order='C' expects the array to be C-contiguous.
🚀 Application
advanced
2: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?
AUse C-order arrays because NumPy's dot product is optimized for row-major contiguous data.
BUse Fortran-order arrays because NumPy's dot product is optimized for column-major contiguous data.
CMemory layout does not affect matrix multiplication performance in NumPy.
DUse a mix of C-order and Fortran-order arrays for best performance.
Attempts:
2 left
💡 Hint
Think about how NumPy stores arrays by default and how BLAS libraries expect data.
🧠 Conceptual
expert
2:00remaining
Effect of memory layout on slicing and views
Which statement about slicing a Fortran-order NumPy array is true?
ASlicing a Fortran-order array along any axis returns a view with contiguous memory in C order.
BSlicing a Fortran-order array always returns a copy, never a view.
CSlicing a Fortran-order array along the first axis returns a view with contiguous memory in Fortran order.
DSlicing a Fortran-order array along the last axis returns a view with contiguous memory in C order.
Attempts:
2 left
💡 Hint
Consider how Fortran-order arrays store data column-wise and how slicing affects contiguity.