Q&A
What is a 3-D array?
Overview
Arrays generalize by dimension: a 1-D array is a list (indexed by one number), a 2-D array is a matrix/grid (rows × columns), and a 3-D array adds a third axis - think of it as depth × rows × columns, like a stack of spreadsheets or the voxels of a cube. They appear in image processing (height × width × color channels), scientific computing, 3-D game grids, and tensors in machine learning.
graph TD
A[3-D Array: depth × rows × cols] --> L0[Layer 0: a 2-D matrix]
A --> L1[Layer 1: a 2-D matrix]
A --> L2[Layer 2: a 2-D matrix]
L0 --> R0["row → cols: arr[0][r][c]"]
style A fill:#1e1b4b,color:#fff
Visualizing It
A 3-D array of size 2 × 3 × 4 holds 2 × 3 × 4 = 24 elements: 2 layers, each a 3-row by 4-column matrix. Address an element with three indices: arr[layer][row][col].
In Java
int[][][] cube = new int[2][3][4]; // 2 layers, 3 rows, 4 cols
cube[0][1][2] = 99; // set one element
System.out.println(cube[0][1][2]); // 99
// Literal initialization:
int[][][] data = {
{ {1, 2}, {3, 4} }, // layer 0 (2x2)
{ {5, 6}, {7, 8} } // layer 1 (2x2)
};
// Traverse with three nested loops:
for (int i = 0; i < data.length; i++)
for (int j = 0; j < data[i].length; j++)
for (int k = 0; k < data[i][j].length; k++)
System.out.println(data[i][j][k]);
In Python (lists or NumPy)
# Nested lists
cube = [[[0]*4 for _ in range(3)] for _ in range(2)]
cube[0][1][2] = 99
# NumPy (preferred for real numeric work)
import numpy as np
arr = np.zeros((2, 3, 4)) # shape: depth, rows, cols
arr[0, 1, 2] = 99
print(arr.shape) # (2, 3, 4)
NumPy treats the three indices as one logical object and supports slicing across any axis (arr[:, 0, :]).
In C
int cube[2][3][4]; // statically sized
cube[0][1][2] = 99;
In C the array is laid out contiguously in row-major order; the compiler computes the flat offset as ((i * rows) + j) * cols + k.
Memory Layout
Although logically 3-D, memory is 1-D. Languages flatten it using a formula. For dimensions D × R × C in row-major order, element [i][j][k] lives at flat index:
offset = i*(R*C) + j*C + k
Understanding this matters for performance (cache locality) and for languages that expose flat buffers.
Common Uses
- Images / video:
[frame][height][width]or[height][width][channel]. - 3-D grids / simulations: voxel data, game maps, cellular automata.
- Machine learning: mini-batches of matrices, color images as tensors.
- Scientific data: 3-D measurements over space.
Why This Matters in Interviews
Be ready to declare and traverse a 3-D array (three nested loops or NumPy slicing), explain the depth × rows × cols mental model, and describe the row-major flattening formula. The offset formula is a favorite follow-up because it reveals whether you understand that "3-D" is a logical view over linear memory.
Common Pitfalls
- Index-order confusion (
[layer][row][col]vs other conventions). - Off-by-one and bounds errors across three loops.
- In Python, building nested lists with
[[0]*4]*3, which creates shared references (use comprehensions instead). - Heavy nested-list math in Python where NumPy would be far faster.
References
- NumPy ndarray docs: https://numpy.org/doc/stable/reference/arrays.ndarray.html
- Java arrays tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html