0
0
NumPydata~20 mins

Memory-mapped arrays for large data in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory-mapped Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of memory-mapped array modification
What is the output of the following code snippet that uses a memory-mapped array?
NumPy
import numpy as np
filename = 'test_mmap.dat'
# Create a memmap file with 5 integers
arr = np.memmap(filename, dtype='int32', mode='w+', shape=(5,))
arr[:] = np.arange(5)
arr.flush()
# Reopen memmap in read-write mode
arr2 = np.memmap(filename, dtype='int32', mode='r+', shape=(5,))
arr2[2] = 100
arr2.flush()
print(arr[:])
A[0 1 100 3 4]
B[0 1 2 3 4]
C[0 1 100 100 4]
D[0 1 2 100 4]
Attempts:
2 left
💡 Hint
Remember that memmap shares the same file on disk, so changes in one memmap reflect in others after flush.
data_output
intermediate
1:30remaining
Number of elements in a memory-mapped array
Given a memory-mapped array created with shape (1000, 1000) and dtype float64, how many elements does it contain?
NumPy
import numpy as np
filename = 'large_mmap.dat'
arr = np.memmap(filename, dtype='float64', mode='w+', shape=(1000, 1000))
print(arr.size)
A2000
B1000
C8000
D1000000
Attempts:
2 left
💡 Hint
The size attribute counts total elements in all dimensions.
🔧 Debug
advanced
1:30remaining
Identify the error when opening a memmap file
What error will this code raise when trying to open a memmap file that does not exist in read-only mode?
NumPy
import numpy as np
filename = 'nonexistent.dat'
arr = np.memmap(filename, dtype='float32', mode='r', shape=(10,))
AValueError
BFileNotFoundError
CTypeError
DNo error, creates a new file
Attempts:
2 left
💡 Hint
Read-only mode requires the file to exist.
🧠 Conceptual
advanced
1:30remaining
Why use memory-mapped arrays for large data?
Which of the following is the main advantage of using memory-mapped arrays for large datasets?
AThey allow processing data larger than RAM by loading only needed parts from disk.
BThey automatically compress data to save disk space.
CThey speed up CPU computations by using GPU acceleration.
DThey convert data into text format for easier reading.
Attempts:
2 left
💡 Hint
Think about how memory-mapping helps with limited memory.
🚀 Application
expert
2:30remaining
Predict the output after multiple memmap modifications
Consider this code that creates and modifies a memory-mapped array multiple times. What is the final printed output?
NumPy
import numpy as np
filename = 'multi_mod.dat'
# Create memmap and initialize
arr = np.memmap(filename, dtype='int16', mode='w+', shape=(4,))
arr[:] = [10, 20, 30, 40]
arr.flush()
# Open second memmap and modify
arr2 = np.memmap(filename, dtype='int16', mode='r+', shape=(4,))
arr2[1] = 200
arr2.flush()
# Modify original memmap without flush
arr[2] = 300
print(arr2[:])
A[10 20 30 40]
B[10 200 300 40]
C[10 200 30 40]
D[10 20 300 40]
Attempts:
2 left
💡 Hint
Changes in one memmap are visible to others only after flush.