Challenge - 5 Problems
Broadcasting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of broadcasting shape check
What is the output of the following code that checks if two arrays can be broadcast together?
NumPy
import numpy as np shape1 = (3, 1, 5) shape2 = (1, 4, 5) result = np.broadcast_shapes(shape1, shape2) print(result)
Attempts:
2 left
💡 Hint
Broadcasting compares shapes from the right side, matching dimensions or using 1 to expand.
✗ Incorrect
The shapes (3,1,5) and (1,4,5) broadcast to (3,4,5) because 1 can expand to match the other dimension.
❓ Predict Output
intermediate2:00remaining
Broadcasting compatibility check result
What happens when you try to broadcast these shapes using numpy.broadcast_shapes?
NumPy
import numpy as np shape1 = (2, 3) shape2 = (3, 2) try: result = np.broadcast_shapes(shape1, shape2) except Exception as e: result = type(e).__name__ print(result)
Attempts:
2 left
💡 Hint
Broadcasting requires dimensions to be equal or one of them to be 1 from the right side.
✗ Incorrect
Shapes (2,3) and (3,2) are incompatible for broadcasting because their trailing dimensions differ and neither is 1.
❓ data_output
advanced2:00remaining
Resulting shape after broadcasting arrays
Given two arrays with shapes (4, 1, 6) and (3, 6), what is the shape of the result after broadcasting?
NumPy
import numpy as np arr1 = np.zeros((4, 1, 6)) arr2 = np.zeros((3, 6)) result = np.broadcast(arr1, arr2) print(result.shape)
Attempts:
2 left
💡 Hint
Broadcasting aligns shapes from the right and expands dimensions of size 1.
✗ Incorrect
The shape (3,6) is treated as (1,3,6) to align with (4,1,6), resulting in (4,3,6).
🔧 Debug
advanced2:00remaining
Identify the broadcasting error
Which option shows the code that will raise a broadcasting error when adding two numpy arrays?
Attempts:
2 left
💡 Hint
Check if dimensions align from the right or are 1 for broadcasting.
✗ Incorrect
Shapes (2,3) and (3,2) cannot broadcast because their trailing dimensions differ and neither is 1.
🚀 Application
expert3:00remaining
Determine output shape for complex broadcasting
You have arrays with shapes (7, 1, 5, 1) and (1, 3, 1, 4). What is the shape of the result after broadcasting these arrays?
NumPy
import numpy as np shape1 = (7, 1, 5, 1) shape2 = (1, 3, 1, 4) result_shape = np.broadcast_shapes(shape1, shape2) print(result_shape)
Attempts:
2 left
💡 Hint
Broadcasting expands dimensions where one shape has 1 and the other has a size >1.
✗ Incorrect
Each dimension is compared from right to left: 1 vs 4 → 4, 5 vs 1 → 5, 1 vs 3 → 3, 7 vs 1 → 7.