Challenge - 5 Problems
Memory-mapped Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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[:])
Attempts:
2 left
💡 Hint
Remember that memmap shares the same file on disk, so changes in one memmap reflect in others after flush.
✗ Incorrect
The first memmap 'arr' writes values 0 to 4. The second memmap 'arr2' modifies index 2 to 100 and flushes. Since both memmaps point to the same file, reading from 'arr' after flush shows the updated value at index 2.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
The size attribute counts total elements in all dimensions.
✗ Incorrect
The array shape is (1000, 1000), so total elements = 1000 * 1000 = 1,000,000.
🔧 Debug
advanced1: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,))
Attempts:
2 left
💡 Hint
Read-only mode requires the file to exist.
✗ Incorrect
Opening a memmap in mode 'r' requires the file to exist. If it does not, Python raises FileNotFoundError.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about how memory-mapping helps with limited memory.
✗ Incorrect
Memory-mapped arrays let you work with data larger than your computer's RAM by loading only the parts you access into memory, avoiding full data loading.
🚀 Application
expert2: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[:])
Attempts:
2 left
💡 Hint
Changes in one memmap are visible to others only after flush.
✗ Incorrect
arr2 modifies index 1 to 200 and flushes, so arr2 sees that change. arr modifies index 2 to 300 but does not flush, so arr2 still sees old value 30 at index 2.