Reflect-pad a 1-D NumPy array without repeating the edge values
Quick Overview
Implement a function that pads a 1-D NumPy array with n mirrored values on each side without repeating the endpoints. It tests the difference between reflect and symmetric padding, index arithmetic with reversed slices, and handling pad widths larger than the array.
Reflect-pad a 1-D NumPy array without repeating the edge values
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `pad_reflect(a, n)` that extends a 1-D NumPy array by `n` reflected values on each side, mirroring around the endpoints without repeating them (`mode='reflect'`, not `'symmetric'`). Return a new array of length `len(a) + 2*n` with the original unchanged in the middle.
Requirements:
- Do not mutate the input.
- `n = 0` returns a copy of the original array.
- Slicing and indexing or `np.pad(a, n, mode='reflect')` may be used.
For example:
```text
Input: a = np.array([1, 2, 3, 4]), n = 2
Output: [3, 2, 1, 2, 3, 4, 3, 2]
```
```hint Map each padded slot
Write out which index of `a` each padded position copies, and check whether the endpoint itself ever appears in the padding.
```
### Constraints & Assumptions
- `n ≥ 0`
- `len(a) ≥ 2` when `n > 0`
### What a Strong Answer Covers
- A correct result for the example and for `n = 0`, with the input left unchanged.
- The difference between reflecting about the endpoint and mirroring that repeats it.
- What happens when `n` is larger than `len(a) - 1`, and how a manual implementation handles it.
- Behavior on very short inputs (length 1 or empty).
### Follow-up Questions
- How does `'reflect'` differ from `'symmetric'`?
- What happens when `n` exceeds `len(a) - 1`?
- Where does reflect padding come up in practice, for example when smoothing a signal or filtering an image?
Overview: Implement a function that pads a 1-D NumPy array with n mirrored values on each side without repeating the endpoints. It tests the difference between reflect and symmetric padding, index arithmetic with reversed slices, and handling pad widths larger than the array.
Implement pad_reflect(a, n) that extends a 1-D NumPy array by n reflected values on each side, mirroring around the endpoints without repeating them (mode='reflect', not 'symmetric'). Return a new array of length len(a) + 2*n with the original unchanged in the middle.
Requirements:
Do not mutate the input.
n = 0
returns a copy of the original array.
Slicing and indexing or
np.pad(a, n, mode='reflect')
may be used.
For example:
Input: a = np.array([1, 2, 3, 4]), n = 2
Output: [3, 2, 1, 2, 3, 4, 3, 2]
Constraints & Assumptions
n ≥ 0
len(a) ≥ 2
when
n > 0
What a Strong Answer Covers Guidance
A correct result for the example and for
n = 0
, with the input left unchanged.
The difference between reflecting about the endpoint and mirroring that repeats it.
What happens when
n
is larger than
len(a) - 1
, and how a manual implementation handles it.
Behavior on very short inputs (length 1 or empty).
Follow-up Questions Guidance
How does
'reflect'
differ from
'symmetric'
?
What happens when
n
exceeds
len(a) - 1
?
Where does reflect padding come up in practice, for example when smoothing a signal or filtering an image?