Challenge - 5 Problems
Type Promotion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of adding int32 and float64 arrays
What is the data type of the resulting array after adding these two NumPy arrays?
NumPy
import numpy as np arr1 = np.array([1, 2, 3], dtype=np.int32) arr2 = np.array([1.5, 2.5, 3.5], dtype=np.float64) result = arr1 + arr2 print(result.dtype)
Attempts:
2 left
💡 Hint
When adding integers and floats, NumPy promotes to the more general type.
✗ Incorrect
NumPy promotes the integer array to float64 to match the float64 array, so the result is float64.
❓ Predict Output
intermediate2:00remaining
Result type of multiplying uint8 and int16 arrays
What is the data type of the resulting array after multiplying these two NumPy arrays?
NumPy
import numpy as np arr1 = np.array([10, 20, 30], dtype=np.uint8) arr2 = np.array([1000, 2000, 3000], dtype=np.int16) result = arr1 * arr2 print(result.dtype)
Attempts:
2 left
💡 Hint
NumPy promotes the narrower dtype to the wider one.
✗ Incorrect
The uint8 array is promoted to int32 to safely hold the result with int16, so the result is int32.
❓ data_output
advanced2:30remaining
Output array and dtype after adding complex64 and float32 arrays
What is the output array and its data type after adding these two arrays?
NumPy
import numpy as np arr1 = np.array([1+2j, 3+4j], dtype=np.complex64) arr2 = np.array([1.5, 2.5], dtype=np.float32) result = arr1 + arr2 print(result) print(result.dtype)
Attempts:
2 left
💡 Hint
Adding float32 to complex64 promotes float32 to complex64.
✗ Incorrect
NumPy promotes float32 to complex64 to match complex64, so output dtype is complex64.
❓ Predict Output
advanced2:00remaining
Data type after subtracting int64 and uint64 arrays
What is the data type of the resulting array after subtracting these two arrays?
NumPy
import numpy as np arr1 = np.array([10, 20, 30], dtype=np.int64) arr2 = np.array([1, 2, 3], dtype=np.uint64) result = arr1 - arr2 print(result.dtype)
Attempts:
2 left
💡 Hint
Subtracting signed and unsigned 64-bit integers promotes to float64 to avoid overflow.
✗ Incorrect
NumPy promotes int64 and uint64 to float64 to safely represent the result.
❓ visualization
expert3:00remaining
Visualizing type promotion in mixed dtype operations
Which option shows the correct plot of the data types resulting from adding arrays of these dtypes: int8, uint16, float32, complex128?
NumPy
import numpy as np import matplotlib.pyplot as plt dtypes = [np.int8, np.uint16, np.float32, np.complex128] results = [] for i, dt1 in enumerate(dtypes): for j, dt2 in enumerate(dtypes): a = np.array([1], dtype=dt1) b = np.array([1], dtype=dt2) c = a + b results.append((dt1.__name__, dt2.__name__, c.dtype.name)) # Prepare data for heatmap import pandas as pd pivot_data = pd.DataFrame(results, columns=['Type1', 'Type2', 'Result']) pivot_table = pivot_data.pivot('Type1', 'Type2', 'Result') plt.figure(figsize=(8,6)) plt.title('Resulting dtype from adding two dtypes') plt.imshow(pivot_table.applymap(lambda x: {'int8':0,'uint16':1,'float32':2,'complex128':3}[x]), cmap='viridis') plt.xticks(range(len(dtypes)), [dt.__name__ for dt in dtypes]) plt.yticks(range(len(dtypes)), [dt.__name__ for dt in dtypes]) plt.colorbar(ticks=[0,1,2,3], label='dtype code') plt.show()
Attempts:
2 left
💡 Hint
NumPy promotes to the smallest dtype that can hold both inputs without loss.
✗ Incorrect
NumPy promotes types to the more general type that can hold both inputs, e.g., int8 + uint16 → uint16, float32 + complex128 → complex128.