0
0
NumPydata~20 mins

Contiguous memory layout concept in NumPy - Practice Problems & Coding Challenges

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
Check if a NumPy array is contiguous
Given the code below, what will be the output of the print statement?
NumPy
import numpy as np
arr = np.arange(12).reshape(3,4)
print(arr.flags['C_CONTIGUOUS'])
ATrue
BFalse
CNone
DRaises an error
Attempts:
2 left
💡 Hint
Think about how reshape affects memory layout by default.
Predict Output
intermediate
2:00remaining
Effect of transpose on memory contiguity
What will be the output of this code snippet?
NumPy
import numpy as np
arr = np.arange(6).reshape(2,3)
arr_t = arr.T
print(arr_t.flags['C_CONTIGUOUS'])
ATrue
BNone
CRaises ValueError
DFalse
Attempts:
2 left
💡 Hint
Transpose changes the order of elements in memory.
data_output
advanced
2:00remaining
Memory layout after slicing
Consider the following code. How many elements does the resulting array have, and is it contiguous in memory?
NumPy
import numpy as np
arr = np.arange(10)
sliced = arr[::2]
print(len(sliced), sliced.flags['C_CONTIGUOUS'])
A5, False
B10, False
C10, True
D5, True
Attempts:
2 left
💡 Hint
Slicing with a step creates a view but may break contiguity.
visualization
advanced
3:00remaining
Visualizing memory layout flags
Which option correctly shows the output of the following code?
NumPy
import numpy as np
arr = np.arange(6).reshape(2,3)
arr_slice = arr[:, 1:3]
print(arr_slice.flags)
A
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
B
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
C
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
D
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
Attempts:
2 left
💡 Hint
Slicing columns keeps row-major order but may break contiguity.
🧠 Conceptual
expert
3:00remaining
Understanding memory layout impact on performance
Which statement best explains why contiguous memory layout improves performance in NumPy?
AContiguous arrays automatically compress data to save memory space.
BContiguous arrays reduce the number of CPU cores needed to process data.
CContiguous arrays allow faster access because data is stored sequentially in memory, improving cache usage.
DContiguous arrays enable parallel processing without any synchronization.
Attempts:
2 left
💡 Hint
Think about how computers read memory and the role of CPU cache.