Shape Compatibility Practice Problem
This data science coding problem helps you practice Broadcasting & Vectorization, shape compatibility, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Broadcasting & Vectorization.
- Problem ID: 109
- Problem key: 109-shape-compatibility
- URL: https://datacrack.app/solve/109-shape-compatibility
- Difficulty: easy
- Topic: Broadcasting & Vectorization
- Module: NumPy Foundations
Problem Statement
# 🧩 Shape Compatibility
---
### 🎯 Goal
Before two arrays can be broadcast together, their shapes must be **compatible**. This problem teaches you to walk through the broadcasting rules manually — a skill that prevents shape mismatch errors in real NumPy, Pandas, and ML code. You will implement the exact algorithm NumPy uses to determine compatibility and compute the resulting shape.
---
### 🔍 Shape Compatibility
Broadcasting compares shapes **from right to left**.
```python
(5, 1, 3)
(1, 4, 1)
```
| Pair | Result |
|:--|:--|
| `3` and `1` | `3` |
| `1` and `4` | `4` |
| `5` and `1` | `5` |
Final shape:
```python
(5, 4, 3)
```
Rules:
| Pair | Result |
|:--|:--|
| same numbers | keep it |
| one is `1` | use the other |
| different and neither is `1` | incompatible |
---
### 💻 Task
Implement `check_broadcast_compatible(shape_a, shape_b)` that returns the resulting broadcast shape as a list, or `None` if the shapes are incompatible.
---
### 📥 Input
- `shape_a`: list of ints (first array shape)
- `shape_b`: list of ints (second array shape)
### 📤 Output
- List of ints (broadcast result shape), or `None` if incompatible
---
### 🧩 Starter Code
```python
def check_broadcast_compatible(shape_a, shape_b):
"""
Check if two shapes are broadcast-compatible.
Args:
shape_a (list): Shape of the first array
shape_b (list): Shape of the second array
Returns:
list or None: Resulting broadcast shape, or None if incompatible
"""
# 🧠 TODO: Pad shorter shape with 1s on the left
# 🧠 TODO: Compare each dimension pair
# 🧠 TODO: Return result shape or None
pass
```
---
### 💡 Example
```python
check_broadcast_compatible([3, 4], [4]) # Expected: [3, 4]
check_broadcast_compatible([3, 1], [1, 4]) # Expected: [3, 4]
check_broadcast_compatible([3], [4]) # Expected: None
```
---
### 🔑 Key Concepts
- This is the **exact algorithm** behind `np.broadcast_shapes()` and every NumPy arithmetic operation
- Understanding it prevents the common `ValueError: operands could not be broadcast together` error
- Broadcasting only virtually stretches dimensions of size 1 — it does not physically copy dataStarter Code
def check_broadcast_compatible(shape_a, shape_b):
"""
Check if two shapes are broadcast-compatible.
Args:
shape_a (list): Shape of the first array
shape_b (list): Shape of the second array
Returns:
list or None: Resulting broadcast shape, or None if incompatible
"""
# 🧠 TODO: Pad shorter shape with 1s on the left
# 🧠 TODO: Compare each dimension pair
# 🧠 TODO: Return result shape or None
pass