0
0
NumPydata~20 mins

Views share memory with originals in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Mastery in NumPy
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of modifying a NumPy view
What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3, 4])
view = arr[1:3]
view[0] = 10
print(arr)
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
view = arr[1:3]
view[0] = 10
print(arr)
A[1 2 3 4]
B[1 10 3 4]
C[10 2 3 4]
D[1 10 10 4]
Attempts:
2 left
💡 Hint
Remember that views share memory with the original array.
data_output
intermediate
1:30remaining
Memory sharing check with np.shares_memory
Given the code below, what does the print statement output?
import numpy as np
x = np.arange(5)
y = x[2:]
print(np.shares_memory(x, y))
NumPy
import numpy as np
x = np.arange(5)
y = x[2:]
print(np.shares_memory(x, y))
AFalse
BNone
CTrue
DRaises an error
Attempts:
2 left
💡 Hint
Slices usually share memory with the original array.
🔧 Debug
advanced
2:00remaining
Why does modifying a copy not affect the original?
Consider this code:
import numpy as np
arr = np.array([5, 6, 7, 8])
copied = arr.copy()
copied[0] = 100
print(arr)

Why does the original array not change?
NumPy
import numpy as np
arr = np.array([5, 6, 7, 8])
copied = arr.copy()
copied[0] = 100
print(arr)
ABecause modifying copied modifies arr internally
BBecause copied is a view sharing memory with arr
CBecause arr is immutable
DBecause copied is a separate copy with its own memory
Attempts:
2 left
💡 Hint
Think about what .copy() does to the data.
🚀 Application
advanced
2:30remaining
Detecting if two arrays share memory
You have two arrays:
import numpy as np
base = np.array([1, 2, 3, 4, 5])
arr1 = base[1:4]
arr2 = base.copy()[1:4]

Which code snippet correctly detects which arrays share memory with base?
NumPy
import numpy as np
base = np.array([1, 2, 3, 4, 5])
arr1 = base[1:4]
arr2 = base.copy()[1:4]
Aprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # True False
Bprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # False False
Cprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # False True
Dprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # True True
Attempts:
2 left
💡 Hint
Remember that slicing creates views but copy() creates independent arrays.
🧠 Conceptual
expert
3:00remaining
Effect of modifying a view on a reshaped array
Given the code below, what is the output?
import numpy as np
original = np.arange(6)
reshaped = original.reshape(2, 3)
view = reshaped[:, 1]
view[0] = 99
print(original)
NumPy
import numpy as np
original = np.arange(6)
reshaped = original.reshape(2, 3)
view = reshaped[:, 1]
view[0] = 99
print(original)
A[0 99 2 3 4 5]
B[0 1 2 3 4 5]
C[0 1 99 3 4 5]
D[99 1 2 3 4 5]
Attempts:
2 left
💡 Hint
Think about how reshaping and slicing create views sharing memory.