TRT
Sign in
easyFoundationsconcept

How is a two-dimensional array stored in memory?

Explain row-major and column-major layout, the index formula, arrays of arrays, and why the layout decides how fast a loop runs.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstIndex arithmeticAn array index is an offset into a block of memory. The same arithmetic flattens grids, finds neighbors, wraps around the end and turns a position back into a row and column.

A strong answer

Memory is one-dimensional, so a grid is flattened into a line. In row-major order, used by C, C++, NumPy (by default) and most languages, the rows are stored one after another, so element (r, c) of a grid with cols columns is at flat index r × cols + c, and its address is base + (r × cols + c) × element_size. In column-major order, used by Fortran, MATLAB, Julia and R, the columns are stored one after another and the index is c × rows + r. Going back from a flat index is one division: in row-major order, the quotient by cols is the row and the remainder is the column.

Some languages don't use a single block at all. A Java int[][] or a Python list of lists is an array of references to separately allocated rows, which can even have different lengths. That costs an extra pointer per row access and loses contiguity between rows. C's int a[R][C] and NumPy arrays are one flat block.

The layout matters for speed because memory arrives in 64-byte cache lines. A loop whose inner index walks along the contiguous dimension reads consecutive addresses and uses every byte of each line; a loop that walks the other way jumps a whole row per step and can be several times slower on large matrices. NumPy goes further and describes every array by its strides, the bytes to step per dimension, so a transpose or a slice is a new view over the same memory rather than a copy.

What the interviewer is listening for

  • The index formula, in both directions.
  • Row-major versus column-major, with examples of languages.
  • The difference between a flat block and an array of row pointers.
  • The performance consequence for loop order.

Common mistakes

  • Assuming every 2D array is one contiguous block.
  • Mixing up which index is multiplied by the row length.
  • Forgetting that the loop order should match the layout of the language you are in.

This answer walks

Practice follow-ups

  1. 01How does NumPy transpose a large matrix without copying it?
  2. 02In Java, can the rows of an int[][] have different lengths? What does that tell you about its layout?
  3. 03Which loop order would you use to sum a Fortran array, and why?
  4. 04Given a flat index into a 3D array of shape (d, r, c), how do you recover the three coordinates?

Not started

Sign in to save your learning progress.

Sign in to save