Add a Zero Border Around a 2-D NumPy Array with np.pad
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `add_zero_border(a)` that wraps a 2-D NumPy array of shape `(h, w)` in a one-cell-thick border of zeros and returns an array of shape `(h + 2, w + 2)`.
- The outermost ring is all zeros and the interior equals `a`.
- Preserve the input dtype.
For example:
```python
a = np.array([[1, 2],
[3, 4]])
add_zero_border(a)
# array([[0, 0, 0, 0],
# [0, 1, 2, 0],
# [0, 3, 4, 0],
# [0, 0, 0, 0]])
```
```hint Watch the dtype
If you build the output array yourself, check which dtype NumPy gives a new array of zeros by default.
```
### What a Strong Answer Covers
- The output shape and where the original values sit inside it.
- A vectorized implementation, with the pad width and fill value it uses.
- How the result keeps the input dtype.
- Edge cases such as an empty array or an input with more than two dimensions.
### Follow-up Questions
- How would you add a border thicker than one cell, or a different constant?
- How would you write this without `np.pad`?
- For an image of shape `(h, w, c)`, how would you pad only the two spatial axes?
- Why do convolution layers pad their inputs, and how does zero padding differ from reflect padding at the edges?
Overview: Implement a NumPy function that surrounds a 2-D array with a one-cell border of zeros and returns an array two rows and two columns larger. It tests array padding, keeping the input dtype, slicing and handling of multi-channel or empty input.