Interleave two equal-length NumPy arrays elementwise without a Python loop
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `interleave(a, b)` that takes two equal-length 1-D NumPy arrays and returns a 1-D array of length `2n` ordered `a[0], b[0], a[1], b[1], ...`. Use vectorized ops only (for example, stacking then reshaping), with no Python loop over elements.
```python
a = np.array([1, 3, 5])
b = np.array([2, 4, 6])
interleave(a, b) # -> array([1, 2, 3, 4, 5, 6])
```
```hint Think in two dimensions
Picture an intermediate 2-D array where each row holds one pair `(a[i], b[i])`. Then think about the order NumPy reads elements in when it flattens that array.
```
### Constraints & Assumptions
- `a` and `b` are 1-D and of equal length `n`.
### What a Strong Answer Covers
- A fully vectorized construction that returns exactly `2n` elements in the order `a[0], b[0], a[1], b[1], ...`
- Why the flattening order (row-major) produces that sequence
- Time and space complexity
- Input checks and edge cases: mismatched lengths, empty arrays, inputs that are not 1-D, and mixed dtypes
### Follow-up Questions
- How would you generalize to interleaving `k` arrays?
- What if the arrays have different lengths?
- Can you build the result without the intermediate 2-D array, for example by writing into a preallocated output?
Overview: Write a vectorized NumPy function that interleaves two equal-length 1-D arrays into one array ordered a[0], b[0], a[1], b[1] and so on. It tests array shapes, flattening order and input validation, and asks how the idea extends to k arrays or arrays of different lengths.