Challenge - 5 Problems
NumPy Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Comparing speed of element-wise addition
What is the output of the following code showing the time taken for element-wise addition using Python lists vs NumPy arrays?
NumPy
import time import numpy as np size = 1000000 list1 = list(range(size)) list2 = list(range(size)) start = time.time() result_list = [list1[i] + list2[i] for i in range(size)] end = time.time() list_time = end - start arr1 = np.arange(size) arr2 = np.arange(size) start = time.time() result_np = arr1 + arr2 end = time.time() np_time = end - start print(f"List time: {list_time:.4f} seconds") print(f"NumPy time: {np_time:.4f} seconds")
Attempts:
2 left
💡 Hint
Think about how Python loops compare to NumPy's optimized operations.
✗ Incorrect
NumPy uses optimized C code and vectorized operations, making element-wise operations much faster than Python loops over lists.
❓ data_output
intermediate2:00remaining
Memory usage difference between list and NumPy array
What is the output of the following code comparing memory usage of a Python list and a NumPy array of the same size?
NumPy
import sys import numpy as np size = 1000000 list1 = list(range(size)) arr1 = np.arange(size) list_mem = sys.getsizeof(list1) + sum(sys.getsizeof(x) for x in list1) arr_mem = arr1.nbytes print(f"List memory: {list_mem} bytes") print(f"NumPy array memory: {arr_mem} bytes")
Attempts:
2 left
💡 Hint
Consider how Python lists store references vs NumPy arrays store raw data.
✗ Incorrect
Python lists store pointers to objects, which uses more memory. NumPy arrays store data in a compact, typed format, using less memory.
🧠 Conceptual
advanced2:00remaining
Why does NumPy use less memory than Python lists?
Which reason best explains why NumPy arrays use less memory than Python lists?
Attempts:
2 left
💡 Hint
Think about how data is stored in memory for each type.
✗ Incorrect
NumPy arrays store data in fixed-type, contiguous blocks, reducing overhead. Lists store references to Python objects, increasing memory use.
🔧 Debug
advanced2:00remaining
Identifying error in mixed-type NumPy array creation
What error or behavior occurs when running this code?
NumPy
import numpy as np arr = np.array([1, 2, '3', 4]) print(arr.dtype) print(arr + 1)
Attempts:
2 left
💡 Hint
Check how NumPy handles mixed data types in arrays.
✗ Incorrect
NumPy converts all elements to a common type. Here, presence of a string causes all elements to become strings. Adding 1 to strings causes a TypeError.
🚀 Application
expert2:00remaining
Choosing NumPy for large-scale numerical computations
You need to perform matrix multiplication on two large datasets efficiently. Which reason best supports choosing NumPy arrays over Python lists?
Attempts:
2 left
💡 Hint
Think about speed and memory when working with large numerical data.
✗ Incorrect
NumPy uses optimized C code and vectorized operations for fast and memory-efficient matrix math. Python lists are slower and less efficient for such tasks.