All interview guides

NumPy Interview Questions and Answers

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 bank

1. What is fancy indexing?

Intermediate

Answer: 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.

2. What is vectorization?

Advanced

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.

3. What is NumPy?

Beginner

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.

4. What is the main data structure in NumPy?

Beginner

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.

5. What is boolean indexing?

Intermediate

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.

6. What is memory layout (C vs F order)?

Advanced

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.

7. What does np.where() do?

Intermediate

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.

8. What is np.stride_tricks?

Advanced

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.

9. How do you import NumPy?

Beginner

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.

10. What are structured arrays?

Advanced

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'].

11. What is the axis parameter?

Intermediate

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.

12. What is np.einsum()?

Advanced

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.

13. What does keepdims parameter do?

Intermediate

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).

14. What is np.ufunc?

Advanced

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.

15. What is np.dot()?

Intermediate

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.

16. What does np.ones() do?

Beginner

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.

17. What does ufunc.reduce() do?

Advanced

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.

18. What is the @ operator?

Intermediate

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.

19. What is array shape?

Beginner

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.

20. What is np.frompyfunc()?

Advanced

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.

21. What does np.transpose() do?

Intermediate

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.

22. What is np.linalg.inv()?

Intermediate

Answer: Computes matrix inverse

np.linalg.inv(matrix) computes inverse of square matrix. Raises error if singular (non-invertible). Check: A @ A_inv ≈ identity.

23. What is np.vectorize()?

Advanced

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.

24. What is memory mapping?

Advanced

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.

Ready to test yourself?

The full NumPy bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the NumPy quiz