1. What is fancy indexing?
IntermediateAnswer: Indexing with arrays of indices
Fancy indexing uses arrays of indices to access elements: arr[[0, 2, 4]]. Always returns copy, not view. Can use multiple arrays for multi-dimensional indexing.
24 questions that come up in NumPy technical interviews, each with the answer and an explanation of why it is right.
Test yourself — 90 question bankAnswer: Indexing with arrays of indices
Fancy indexing uses arrays of indices to access elements: arr[[0, 2, 4]]. Always returns copy, not view. Can use multiple arrays for multi-dimensional indexing.
Answer: Replacing loops with array operations
Vectorization replaces explicit loops with array operations. Leverages optimized C code. Much faster than Python loops. Core NumPy optimization strategy.
Answer: Numerical Python library for arrays and matrices
NumPy is the fundamental package for scientific computing in Python. It provides support for large multi-dimensional arrays and matrices, along with mathematical functions.
Answer: ndarray (N-dimensional array)
The ndarray (N-dimensional array) is NumPy's core data structure. It's a homogeneous, multi-dimensional container for fixed-size items.
Answer: Indexing with boolean arrays
Boolean indexing uses boolean arrays: arr[arr > 5]. Creates mask of True/False. Returns elements where mask is True. Powerful for filtering.
Answer: C-contiguous (row-major) vs F-contiguous (column-major)
C-order (row-major): last axis changes fastest. F-order (column-major, Fortran): first axis changes fastest. Affects performance. Check with arr.flags.
Answer: Returns indices where condition is True
np.where(condition) returns indices where True. np.where(condition, x, y) returns elements from x where True, y where False. Ternary operation.
Answer: Manipulates array strides for custom views
np.lib.stride_tricks allows custom stride manipulation. Create views without copying data. Powerful but dangerous - easy to create invalid memory access.
Answer: All of the above
All methods work, but import numpy as np is the standard convention. It provides a shorter alias while avoiding namespace pollution.
Answer: Arrays with named fields of different types
Structured arrays have named fields with different types: dtype=[('name', 'U10'), ('age', 'i4')]. Like database table rows. Access fields: arr['name'].
Answer: Specifies dimension for operation
axis specifies dimension: axis=0 for columns (down rows), axis=1 for rows (across columns). axis=None for entire array. Essential for aggregations.
Answer: Einstein summation convention for array operations
np.einsum() performs operations using Einstein summation. Concise notation for complex operations. Example: np.einsum('ij,jk->ik', A, B) for matrix multiplication.
Answer: Keeps original number of dimensions
keepdims=True keeps original dimensions after aggregation. Result has size 1 in reduced dimension. Useful for broadcasting. Example: arr.sum(axis=0, keepdims=True).
Answer: Universal function operating element-wise
ufuncs are universal functions operating element-wise with broadcasting. Examples: np.add, np.multiply. Optimized C implementations. Support methods like reduce, accumulate.
Answer: Matrix multiplication / dot product
np.dot(a, b) computes dot product (1D), matrix multiplication (2D). @ operator equivalent in Python 3.5+. Inner dimensions must match.
Answer: Creates array filled with ones
np.ones(shape) creates array filled with ones. Example: np.ones((2, 3)) creates 2x3 array of ones. Specify dtype with dtype parameter.
Answer: Applies ufunc cumulatively to reduce array
ufunc.reduce(arr) applies operation cumulatively. np.add.reduce(arr) same as sum. Can specify axis. Foundation for many aggregation operations.
Answer: Matrix multiplication operator
@ is matrix multiplication operator: A @ B. Equivalent to np.dot(A, B) or np.matmul(A, B). Cleaner syntax for matrix operations.
Answer: Tuple of dimensions
Shape is tuple of dimensions. Access with .shape attribute: arr.shape returns (rows, cols) for 2D. (3, 4) means 3 rows, 4 columns.
Answer: Creates ufunc from Python function
np.frompyfunc(func, nin, nout) creates ufunc from Python function. Slower than native ufuncs but enables custom element-wise operations. Returns object arrays.
Answer: Transposes array (swaps axes)
np.transpose(arr) or arr.T transposes array. Swaps axes: (3, 4) becomes (4, 3). For 1D, no change. Specify axes for custom permutation.
Answer: Computes matrix inverse
np.linalg.inv(matrix) computes inverse of square matrix. Raises error if singular (non-invertible). Check: A @ A_inv ≈ identity.
Answer: Vectorizes function (convenience, not performance)
np.vectorize() vectorizes function for convenience, not performance. Essentially loops in C. Use for clean code with mixed types. For speed, write vectorized code.
Answer: Maps files to memory for large array handling
Memory mapping (np.memmap) maps files to memory. Access large arrays without loading entire file. Changes written to disk. Efficient for big data.
The full NumPy bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the NumPy quiz