Build Coordinate Matrices with NumPy meshgrid
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `coord_grid(x, y)`: given 1-D arrays `x` and `y`, return the tuple `(X, Y)` of two `np.ndarray` coordinate matrices produced by `np.meshgrid` with default `indexing='xy'`. Both have shape `(len(y), len(x))`: `X` varies along columns (every row is a copy of `x`), `Y` along rows (every column is a copy of `y`).
For example:
```python
x = np.array([1, 2, 3])
y = np.array([10, 20])
X, Y = coord_grid(x, y)
X # shape (2, 3)
# [[1, 2, 3],
# [1, 2, 3]]
Y # shape (2, 3)
# [[10, 10, 10],
# [20, 20, 20]]
```
```hint Rows and columns
Under the default indexing, work out which input ends up along the rows and which along the columns, then check the shape against `(len(y), len(x))`.
```
### Constraints & Assumptions
- `x` and `y` are 1-D arrays of any length.
- Use `np.meshgrid` with its default `indexing='xy'`.
- Return a tuple of two arrays, `X` first.
### What a Strong Answer Covers
- How the argument order and the default `'xy'` indexing produce shape `(len(y), len(x))`.
- What `X` and `Y` hold row by row and column by column, checked against the example.
- Returning a tuple of two arrays.
- The memory cost of full grids and when a lighter form is enough.
### Follow-up Questions
- How does `indexing='ij'` differ from `'xy'`?
- What does `sparse=True` give you and why use it?
- How would you turn the two grids into an array of (x, y) points, for example to score a model at every grid point?
Overview: Build the two coordinate matrices that np.meshgrid returns for 1-D x and y arrays under the default xy indexing. It tests output shapes and argument order, the difference from ij indexing, and memory-saving sparse grids that rely on broadcasting.