Shuffle the rows of a NumPy array reproducibly with a seed
Quick Overview
Implement a NumPy function that returns a copy of a 2-D array with its rows reordered by a seeded random generator. It tests the Generator API, the difference between copying and in-place shuffles, and how to make random output reproducible from call to call.
Shuffle the rows of a NumPy array reproducibly with a seed
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `shuffled_rows(a: np.ndarray, seed: int) -> np.ndarray` that returns a new 2-D array with the rows of `a` randomly permuted.
Requirements:
- Build the generator with `rng = np.random.default_rng(seed)`.
- Permute rows along axis 0 using `rng.permutation`.
- Return a copy: `a` must not be modified.
- The same `seed` must produce the same output every call.
Example:
```python
a = np.array([[1, 2],
[3, 4],
[5, 6]])
out = shuffled_rows(a, seed=0)
# out holds the rows [1, 2], [3, 4] and [5, 6], each intact, in an order fixed by the seed
# calling shuffled_rows(a, seed=0) again returns the same order
# a is unchanged
```
```hint Copy or in place
NumPy's `Generator` has several methods that reorder data. Check which ones return a new array, which ones modify their argument, and whether each one keeps a row's values together.
```
### What a Strong Answer Covers
- Creates the seeded generator inside the function so every call with the same seed starts from the same state
- Moves whole rows along axis 0 so each row's values stay together
- Leaves the input untouched and explains why the chosen method returns a copy
- Names the reordering methods that would break a requirement, such as shuffling in place or shuffling each column independently
- Handles edge cases such as an array with zero rows or one row
### Follow-up Questions
- How would you shuffle columns instead of rows?
- What is the advantage of `np.random.default_rng` over `np.random.seed`?
- How would you shuffle a feature matrix `X` and a label vector `y` so their rows stay aligned?
- If `a` is too large to copy, how would you shuffle it, and how does that change the function's contract?
Overview: Implement a NumPy function that returns a copy of a 2-D array with its rows reordered by a seeded random generator. It tests the Generator API, the difference between copying and in-place shuffles, and how to make random output reproducible from call to call.
Implement shuffled_rows(a: np.ndarray, seed: int) -> np.ndarray that returns a new 2-D array with the rows of a randomly permuted.
Requirements:
Build the generator with
rng = np.random.default_rng(seed)
.
Permute rows along axis 0 using
rng.permutation
.
Return a copy:
a
must not be modified.
The same
seed
must produce the same output every call.
Example:
a = np.array([[1, 2],
[3, 4],
[5, 6]])
out = shuffled_rows(a, seed=0)
# out holds the rows [1, 2], [3, 4] and [5, 6], each intact, in an order fixed by the seed
# calling shuffled_rows(a, seed=0) again returns the same order
# a is unchanged
What a Strong Answer Covers Guidance
Creates the seeded generator inside the function so every call with the same seed starts from the same state
Moves whole rows along axis 0 so each row's values stay together
Leaves the input untouched and explains why the chosen method returns a copy
Names the reordering methods that would break a requirement, such as shuffling in place or shuffling each column independently
Handles edge cases such as an array with zero rows or one row
Follow-up Questions Guidance
How would you shuffle columns instead of rows?
What is the advantage of
np.random.default_rng
over
np.random.seed
?
How would you shuffle a feature matrix
X
and a label vector
y
so their rows stay aligned?
If
a
is too large to copy, how would you shuffle it, and how does that change the function's contract?