Bird
Raised Fist0
NumPydata~20 mins

Broadcasting compatibility check in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Broadcasting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A(3, 4, 5)
B(3, 1, 5, 4, 5)
CValueError: shape mismatch: objects cannot be broadcast to a single shape
D(3, 4)
Attempts:
2 left
💡 Hint
Broadcasting compares shapes from the right side, matching dimensions or using 1 to expand.
❓ Predict Output
intermediate
2: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)
A(2, 3, 3, 2)
BValueError
C(3, 3)
DTypeError
Attempts:
2 left
💡 Hint
Broadcasting requires dimensions to be equal or one of them to be 1 from the right side.
❓ data_output
advanced
2: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)
A(4, 1, 6, 3, 6)
B(3, 6)
CValueError
D(4, 3, 6)
Attempts:
2 left
💡 Hint
Broadcasting aligns shapes from the right and expands dimensions of size 1.
🔧 Debug
advanced
2:00remaining
Identify the broadcasting error
Which option shows the code that will raise a broadcasting error when adding two numpy arrays?
A
import numpy as np
x = np.ones((2, 3))
y = np.ones((3, 2))
result = x + y
B
import numpy as np
x = np.ones((5, 1))
y = np.ones((1, 4))
result = x + y
C
import numpy as np
x = np.ones((3, 1, 2))
y = np.ones((1, 4, 2))
result = x + y
D
import numpy as np
x = np.ones((1, 3))
y = np.ones((3, 1))
result = x + y
Attempts:
2 left
💡 Hint
Check if dimensions align from the right or are 1 for broadcasting.
🚀 Application
expert
3: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)
A(7, 1, 5, 4)
B(1, 3, 5, 1)
C(7, 3, 5, 4)
DValueError
Attempts:
2 left
💡 Hint
Broadcasting expands dimensions where one shape has 1 and the other has a size >1.

Practice

(1/5)
1. Which of the following pairs of shapes are compatible for broadcasting in NumPy?
easy
A. (1, 2, 3) and (2, 3, 1)
B. (2, 3) and (3, 2)
C. (4, 5) and (4, 5, 1)
D. (3, 1) and (1, 4)

Solution

  1. Step 1: Understand broadcasting rules

    Broadcasting compares shapes from right to left. Dimensions must be equal or one must be 1.
  2. Step 2: Check each pair

    For (3,1) and (1,4), rightmost dims 1 and 4 are compatible (1 can broadcast to 4), left dims 3 and 1 compatible (1 can broadcast to 3). So compatible.
  3. Final Answer:

    (3, 1) and (1, 4) are compatible -> Option D
  4. Quick Check:

    Broadcast dims right to left, 1 can expand [OK]
Hint: Check shapes from right; 1 or equal dims broadcast [OK]
Common Mistakes:
  • Assuming all different shapes are compatible
  • Not comparing dimensions from right to left
  • Ignoring that 1 can broadcast to any size
2. Which of the following is the correct way to check broadcasting compatibility of shapes (2, 3) and (3,) using NumPy?
easy
A. numpy.broadcast_shapes((2, 3), (3,))
B. numpy.broadcast((2, 3), (3,))
C. numpy.broadcast_shapes([2, 3], 3)
D. numpy.broadcast((2, 3), (3))

Solution

  1. Step 1: Identify the correct function

    NumPy provides numpy.broadcast_shapes to check shape compatibility, which takes tuples as shape arguments.
  2. Step 2: Check argument types

    numpy.broadcast_shapes((2, 3), (3,)) uses numpy.broadcast_shapes with tuples, which is correct. numpy.broadcast_shapes([2, 3], 3) uses a scalar for the second argument, which is invalid for this function.
  3. Final Answer:

    numpy.broadcast_shapes((2, 3), (3,)) -> Option A
  4. Quick Check:

    Use numpy.broadcast_shapes with tuples [OK]
Hint: Use numpy.broadcast_shapes with tuples for shape check [OK]
Common Mistakes:
  • Using numpy.broadcast instead of numpy.broadcast_shapes
  • Passing scalars instead of sequences
  • Missing parentheses or wrong argument types
3. What will be the output of the following code?
import numpy as np
shape = np.broadcast_shapes((4, 1, 3), (1, 5, 3))
print(shape)
medium
A. (4, 1, 3)
B. (4, 5, 3)
C. (1, 5, 3)
D. ValueError: shapes not compatible

Solution

  1. Step 1: Compare shapes dimension-wise from right

    Rightmost dims: 3 and 3 match. Next dims: 1 and 5, 1 broadcasts to 5. Leftmost dims: 4 and 1, 1 broadcasts to 4.
  2. Step 2: Determine broadcasted shape

    Broadcasted shape is (4, 5, 3) combining max of each dimension.
  3. Final Answer:

    (4, 5, 3) -> Option B
  4. Quick Check:

    Broadcast dims max or 1 expands [OK]
Hint: Broadcast dims take max or 1 expands to max [OK]
Common Mistakes:
  • Confusing which dimension broadcasts to which
  • Expecting error when shapes are compatible
  • Mixing up order of dimensions
4. The following code throws an error. What is the cause?
import numpy as np
np.broadcast_shapes((3, 2), (2, 3))
medium
A. Shapes are not compatible for broadcasting
B. Function broadcast_shapes does not exist
C. Tuples must be of same length
D. Missing import statement

Solution

  1. Step 1: Check broadcasting rules for shapes (3, 2) and (2, 3)

    Compare from right: 2 vs 3 are different and neither is 1, so incompatible.
  2. Step 2: Confirm error cause

    Since shapes cannot broadcast, numpy.broadcast_shapes raises ValueError.
  3. Final Answer:

    Shapes are not compatible for broadcasting -> Option A
  4. Quick Check:

    Broadcast dims must be equal or 1 [OK]
Hint: Check dims right to left; mismatch without 1 causes error [OK]
Common Mistakes:
  • Thinking function is missing or typo
  • Assuming tuples must be same length
  • Ignoring broadcasting dimension rules
5. You have arrays with shapes (7, 1, 5) and (1, 3, 1). You want to add them using broadcasting. What will be the resulting shape?
hard
A. (1, 3, 5)
B. (7, 1, 1)
C. (7, 3, 5)
D. Cannot broadcast these shapes

Solution

  1. Step 1: Compare shapes from right to left

    Right dims: 5 and 1, 1 broadcasts to 5. Middle dims: 1 and 3, 1 broadcasts to 3. Left dims: 7 and 1, 1 broadcasts to 7.
  2. Step 2: Combine broadcasted dimensions

    Resulting shape is (7, 3, 5) taking max of each dimension.
  3. Final Answer:

    (7, 3, 5) -> Option C
  4. Quick Check:

    Broadcast dims max or 1 expands [OK]
Hint: Broadcast dims max or 1 expands to max [OK]
Common Mistakes:
  • Mixing up dimension order
  • Assuming shapes must be identical
  • Forgetting 1 can broadcast to any size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard