Flatten a 2-D NumPy array into an independent 1-D copy in row-major order

Quick Overview

Write a NumPy function that flattens a 2-D array into a contiguous 1-D array in row-major order and returns a true copy. It tests copies versus views, how flatten and ravel differ, and how memory layout and the order argument affect the result.

Flatten a 2-D NumPy array into an independent 1-D copy in row-major order

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `flatten_matrix(a)` that takes a 2-D NumPy array and returns a contiguous 1-D array of its elements in row-major (C) order. Requirements: - The result must be a copy, not a view sharing memory with `a`. - Modifying the returned array must leave the original unchanged. Example: ```python a = np.array([[1, 2], [3, 4]]) out = flatten_matrix(a) # out -> array([1, 2, 3, 4]) out[0] = 99 # a is still [[1, 2], [3, 4]] ``` ```hint Views hide in plain sight NumPy has several ways to turn a 2-D array into a 1-D one, and some of them return a view when the memory layout allows it. Test your choice with `np.shares_memory(out, a)` on both `a` and `a.T`. ``` ### What a Strong Answer Covers - Picks a method that always allocates new memory and says why the alternatives can alias the input. - Produces row-major order and knows how the `order` argument changes it. - Shows how to confirm the result is independent, for example with `np.shares_memory` or the array flags. - States the time and memory cost of the copy. ### Follow-up Questions - What's the difference between `ravel()` and `flatten()` here? - How would you flatten in column-major (Fortran) order instead? - When would you deliberately choose `ravel()` over `flatten()`?

Overview: Write a NumPy function that flattens a 2-D array into a contiguous 1-D array in row-major order and returns a true copy. It tests copies versus views, how flatten and ravel differ, and how memory layout and the order argument affect the result.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write flatten_matrix(a) that takes a 2-D NumPy array and returns a contiguous 1-D array of its elements in row-major (C) order.

Requirements:

  • The result must be a copy, not a view sharing memory with a .
  • Modifying the returned array must leave the original unchanged.

Example:

a = np.array([[1, 2], [3, 4]])
out = flatten_matrix(a)
# out -> array([1, 2, 3, 4])
out[0] = 99
# a is still [[1, 2], [3, 4]]

What a Strong Answer Covers Guidance

  • Picks a method that always allocates new memory and says why the alternatives can alias the input.
  • Produces row-major order and knows how the order argument changes it.
  • Shows how to confirm the result is independent, for example with np.shares_memory or the array flags.
  • States the time and memory cost of the copy.

Follow-up Questions Guidance

  • What's the difference between ravel() and flatten() here?
  • How would you flatten in column-major (Fortran) order instead?
  • When would you deliberately choose ravel() over flatten() ?