Convert a C-Contiguous NumPy Array to Fortran (Column-Major) Order

Quick Overview

Return a NumPy array with the same shape and values as a C-contiguous input but stored in column-major (Fortran) order. It tests memory layout, strides and contiguity flags, views versus copies, and why a transpose does not meet the requirement.

Convert a C-Contiguous NumPy Array to Fortran (Column-Major) Order

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `to_fortran(a)`: given a 2-D C-contiguous `ndarray` `a`, return a new array with the same values and shape laid out in Fortran (column-major) order. For example: ```python a = np.array([[1, 2, 3], [4, 5, 6]], order='C') result = to_fortran(a) result.flags['F_CONTIGUOUS'] # True np.array_equal(result, a) # True np.frombuffer(result.tobytes('A'), dtype=result.dtype) # array([1, 4, 2, 5, 3, 6]) the buffer holds the values column by column ``` ```hint Layout versus values Keep two things apart: the shape and indexed values a caller sees, and the order the elements sit in the buffer. Ask which operations change only the second. ``` ### Constraints & Assumptions - `a` is a 2-D C-contiguous `ndarray`. - `result.flags['F_CONTIGUOUS']` is `True`. - `np.array_equal(result, a)` is `True`. ### What a Strong Answer Covers - The difference between an array's shape and indexed values and the order of its elements in memory, including what strides record. - A call that produces a column-major copy, and why its values still compare equal to the input. - Why a transpose on its own does not meet the requirements. - When a copy is made and what it costs in time and memory. ### Follow-up Questions - What is the difference between `np.asfortranarray` and `np.array(a, order='F')`? - Why can a transpose be F-contiguous without copying data? - When does the memory order of an array matter in practice?

Overview: Return a NumPy array with the same shape and values as a C-contiguous input but stored in column-major (Fortran) order. It tests memory layout, strides and contiguity flags, views versus copies, and why a transpose does not meet the requirement.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement to_fortran(a): given a 2-D C-contiguous ndarray a, return a new array with the same values and shape laid out in Fortran (column-major) order.

For example:

a = np.array([[1, 2, 3], [4, 5, 6]], order='C')
result = to_fortran(a)

result.flags['F_CONTIGUOUS']   # True
np.array_equal(result, a)      # True
np.frombuffer(result.tobytes('A'), dtype=result.dtype)
# array([1, 4, 2, 5, 3, 6])    the buffer holds the values column by column

Constraints & Assumptions

  • a is a 2-D C-contiguous ndarray .
  • result.flags['F_CONTIGUOUS'] is True .
  • np.array_equal(result, a) is True .

What a Strong Answer Covers Guidance

  • The difference between an array's shape and indexed values and the order of its elements in memory, including what strides record.
  • A call that produces a column-major copy, and why its values still compare equal to the input.
  • Why a transpose on its own does not meet the requirements.
  • When a copy is made and what it costs in time and memory.

Follow-up Questions Guidance

  • What is the difference between np.asfortranarray and np.array(a, order='F') ?
  • Why can a transpose be F-contiguous without copying data?
  • When does the memory order of an array matter in practice?