NumPy interview questions answer for preparation

Here are 100 NumPy interview questions and answers, organized by category to cover array creation, manipulation, broadcasting, linear algebra, performance, and practical problem-solving.

NumPy Fundamentals

1. What is NumPy?
NumPy (Numerical Python) is a foundational library for numerical computing in Python. It provides a high-performance multidimensional array object (ndarray), and tools for working with these arrays, including mathematical, logical, shape manipulation, and I/O functions.

2. Why is NumPy faster than pure Python lists for numerical operations?
NumPy arrays are stored in contiguous memory, support vectorized operations (written in C), and avoid Python’s per‑element interpreter overhead and dynamic type checking. This allows single‑instruction‑multiple‑data (SIMD) processing.

3. What is an ndarray?
An ndarray is a homogeneous, multidimensional container of items of the same type and size. It is the core data structure of NumPy.

4. What are the basic attributes of an ndarray?
shape (dimensions), ndim (number of dimensions), dtype (data type of elements), size (total number of elements), itemsize (bytes per element), and nbytes (total bytes).

5. How do you create a NumPy array from a Python list?
np.array([1, 2, 3]) creates a 1‑D array. For 2‑D: np.array([[1, 2], [3, 4]]).

6. How do you find the shape and dimensions of an array?
arr.shape and arr.ndim. For example, a = np.ones((3,4)) has shape (3,4) and ndim 2.

7. What are the common data types (dtypes) in NumPy?
int32int64float32float64complex64boolobjectS (string), datetime64, and others. The default float is float64.

8. How do you explicitly set the data type when creating an array?
np.array([1,2,3], dtype=np.float32) or using the string shorthand dtype='f4'.

9. What is the difference between a view and a copy?
A view shares the same underlying data as the original array (modifying one affects the other). A copy creates a completely independent array in new memory. Slicing usually returns a view; functions like np.copy() create a copy.

10. How do you check if an array is a view or copy?
Using arr.base – if it returns None, the array owns its data (copy); if it returns another array, it’s a view onto that base.

Creating Arrays

11. How do you create an array of all zeros?
np.zeros(shape) e.g., np.zeros((3, 5)) for a 3×5 float64 array.

12. How do you create an array of all ones?
np.ones((2,4), dtype=int). You can also use np.ones_like(existing_array) to match shape and type.

13. How do you create an identity matrix?
np.eye(N) or np.identity(N) for a square matrix. np.eye(N, M) for a rectangular one.

14. How do you create an empty array?
np.empty(shape) returns an array with arbitrary (uninitialized) values, which can be faster than zeros/ones when you plan to overwrite all values.

15. How do you create an array with a range of numbers?
np.arange(start, stop, step) (similar to Python’s range), or np.linspace(start, stop, num) for a specified number of evenly spaced values. Example: np.linspace(0, 1, 5) returns [0., 0.25, 0.5, 0.75, 1.].

16. What is the difference between np.arange and np.linspace?
arange uses a step size; you specify the spacing. linspace uses the required number of points and guarantees the exact endpoints, useful for plotting.

17. How do you create a constant array filled with a specific value?
np.full(shape, fill_value) e.g., np.full((2,3), 7).

18. How do you create a 2D array of random numbers?
np.random.random((rows, cols)) for uniform [0,1). np.random.randn(m,n) for standard normal. np.random.randint(low, high, size=(m,n)) for integers.

19. How do you create an array from a function?
np.fromfunction(function, shape) calls the function with indices (e.g., lambda i, j: i + j for a 2D array). Also np.fromiter(iterable, dtype).

20. How do you create a diagonal matrix from a 1D array?
np.diag(a) where a is a 1D array. If a is 2D, it extracts the diagonal.

Array Attributes & Reshaping

21. How do you reshape an array?
arr.reshape(new_shape). The new shape must be compatible with the total number of elements. Use -1 for auto‑calculation: arr.reshape(-1, 1).

22. How do you flatten an array?
arr.flatten() returns a copy, arr.ravel() returns a view when possible (faster). Both produce a 1D array.

23. How do you transpose a 2D array?
arr.T or np.transpose(arr). For higher dimensions, transpose can specify axes order.

24. How do you add a new axis to an array?
arr[np.newaxis, :] or np.expand_dims(arr, axis=0). This turns a 1D array of shape (n,) into (1, n).

25. How do you concatenate two arrays vertically and horizontally?
np.vstack((a, b)) and np.hstack((a, b)). Also np.concatenate((a,b), axis=0) for vertical, axis=1 for horizontal.

26. How do you split an array into several smaller ones?
np.split(arr, num_sections, axis)np.vsplitnp.hsplit, and np.array_split for uneven splits.

27. How do you repeat an array?
np.tile(arr, reps) to repeat the whole array like a tile. np.repeat(arr, n) repeats each element n times.

28. How do you stack arrays depth‑wise (along third axis)?
np.dstack((a, b)). For example, stacking two 2D arrays of the same shape creates a 3D array.

29. How do you swap axes of an array?
np.swapaxes(arr, axis1, axis2) or arr.swapaxes(0,1).

30. What does np.squeeze do?
Removes single‑dimensional entries from the shape of an array, e.g., (3,1,4) → (3,4).

Indexing & Slicing

31. Explain basic slicing in NumPy.
Like Python lists, but for multidimensional arrays: arr[row_start:row_end, col_start:col_end]. Slices return views.

32. How do you select a specific row or column?
arr[2, :] for the third row, arr[:, 1] for the second column. The result is a 1D array.

33. What is boolean indexing?
Using a boolean array of the same shape to filter elements: arr[arr > 5] returns elements > 5 as a 1D array.

34. How do you combine multiple boolean conditions?
Using bitwise operators & (and), | (or), ~ (not) and parentheses: arr[(arr > 2) & (arr < 8)].

35. What is fancy indexing?
Indexing using integer arrays or lists: arr[[0, 2, 4]] returns selected rows. For 2D, arr[[0,1], [2,3]] returns elements at (0,2) and (1,3).

36. How do you set values using boolean indexing?
arr[arr < 0] = 0 sets all negative values to 0 in place.

37. How do you select elements that satisfy multiple criteria across rows/columns?
Combine boolean arrays: arr[(arr[:,0] > 1) & (arr[:,1] < 5)] selects rows where column 0 > 1 and column 1 < 5.

38. How do you extract a submatrix using slices?
arr[1:4, 2:5] gives rows 1–3 and columns 2–4.

39. How do you get the indexes of sorted elements or maximum values?
np.argsort(arr) returns indices that would sort the array. np.argmax(arr, axis=0) returns indices of maximum values along an axis.

40. How do you find the indices where a condition is true?
np.where(condition) returns a tuple of arrays of indices. For a single condition, use np.where(arr > 5) to get indices.

Broadcasting

41. What is broadcasting?
The set of rules by which NumPy handles arithmetic between arrays of different shapes, expanding the smaller array across the larger array “as if” it had matching dimensions, without copying data.

42. What are the broadcasting rules?

  1. If arrays have different numbers of dimensions, prepend 1s to the shape of the smaller.
  2. Two dimensions are compatible if they are equal, or one of them is 1. The array with dimension 1 is stretched.

43. Give an example of broadcasting a scalar.
arr + 5 adds 5 to every element of arr.

44. How does broadcasting work with a column vector and a row vector?
A (3,1) array + a (1,4) array yields a (3,4) array, forming an outer sum.

45. What happens if shapes are incompatible?
An error: ValueError: operands could not be broadcast together. Example: (3,4) with (2,4) cannot broadcast because 3 != 2.

46. How do you make a 1D array broadcastable to a 2D row?
arr[np.newaxis, :] or arr.reshape(1, -1) to give it shape (1, n).

47. Can you broadcast in element‑wise multiplication?
Yes, * uses broadcasting. For matrix multiplication, use @ or np.dot; they have different broadcasting semantics.

48. How do you use broadcasting to standardize features (z‑score normalization)?
(data - mean) / std works because mean and std are scalars or 1D arrays that broadcast across rows.

49. Why is broadcasting efficient?
It avoids copying data; instead, NumPy iterates over the memory as if the array were expanded, using strides to navigate.

50. How do you explicitly broadcast an array to a new shape?
np.broadcast_to(arr, new_shape) returns a read‑only view that broadcasts.

Mathematical & Statistical Functions

51. How do you perform element‑wise addition, subtraction, multiplication, division?
Using operators +-*/. These work on arrays of same shape or broadcastable.

52. What are universal functions (ufuncs)?
Functions that operate element‑wise on ndarrays, implemented in C for speed. Examples: np.sinnp.expnp.sqrtnp.add.

53. How do you compute the sum of all elements?
arr.sum() or np.sum(arr). Specify axis for sums along rows/columns: arr.sum(axis=0).

54. How do you compute the mean, median, standard deviation?
arr.mean()np.median(arr)arr.std(). Use axis parameter for row/column‑wise statistics.

55. How do you find the minimum and maximum values?
arr.min()arr.max(); also arr.argmin()arr.argmax() for indices.

56. What are cumulative sum and product?
arr.cumsum() and arr.cumprod(). They return arrays of the same shape with cumulative values along an axis.

57. How do you calculate the dot product of two 1D arrays?
np.dot(a, b) or a @ b.

58. How do you compute the matrix product of two 2D arrays?
A @ B or np.matmul(A, B). Follows matrix multiplication rules.

59. How do you compute the cross product and outer product?
np.cross(a, b) (for 2‑ or 3‑element vectors). Outer product: np.outer(a, b) (or a[:, None] * b).

60. How do you calculate the determinant of a matrix?
np.linalg.det(A).

61. How do you invert a matrix?
np.linalg.inv(A). If the matrix is singular, it raises a LinAlgError.

62. How do you solve a linear system Ax = b?
np.linalg.solve(A, b). For least‑squares, use np.linalg.lstsq.

63. How do you compute eigenvalues and eigenvectors?
np.linalg.eig(A) returns eigenvalues and eigenvectors. np.linalg.eigh for symmetric/Hermitian matrices (faster, real outputs).

64. What is the trace of a matrix and how do you compute it?
The sum of diagonal elements: np.trace(A) or A.trace().

65. How do you compute the Frobenius norm of a matrix?
np.linalg.norm(A, 'fro') or simply np.linalg.norm(A) for the default Frobenius norm of a 2D array.

66. How do you find the percentile or quantile of an array?
np.percentile(arr, q) or np.quantile(arr, q). Example: np.percentile(arr, 50) is the median.

67. How do you compute the correlation coefficient matrix?
np.corrcoef(arr) for a 2D array where rows are variables and columns are observations. Or column-wise with rowvar=False.

68. How do you generate a histogram?
np.histogram(arr, bins=number_of_bins) returns counts and bin edges. For visualization, use plt.hist.

69. What is the difference between np.mean and np.average?
np.average supports weighted averages via the weights parameter; np.mean is a simple arithmetic mean.

70. How do you compute moving averages?
Using convolution: np.convolve(arr, np.ones(N)/N, mode='valid').

Random Number Generation

71. How do you generate random numbers with NumPy?
Using np.random.default_rng() (modern) or legacy np.random.seed and functions like np.random.rand(). Example: rng = np.random.default_rng(seed=42); rng.random().

72. How do you create a random integer array?
rng.integers(low, high, size=shape).

73. How do you generate standard normal random numbers?
rng.standard_normal(shape) or rng.normal(loc, scale, size).

74. How do you shuffle an array?
rng.shuffle(arr) shuffles in place. rng.permutation(arr) returns a shuffled copy.

75. How do you select random elements from an array?
rng.choice(arr, size, replace=True/False, p=probabilities).

76. What is a random seed and why is it used?
A seed initializes the pseudo‑random number generator to make results reproducible. rng = np.random.default_rng(42).

77. How do you generate random samples from a binomial distribution?
rng.binomial(n, p, size).

78. How do you generate random samples from a specific probability distribution?
NumPy provides many: rng.uniformrng.normalrng.exponentialrng.poissonrng.gamma, etc.

79. How do you create a random array of floats between 0 and 1?
rng.random(shape) or rng.uniform(0, 1, shape).

80. How do you set a random state globally?
Best practice is to use a local Generator instance; legacy global state can be set with np.random.seed(0) but is no longer recommended.

Performance, Memory & Strides

81. How does NumPy achieve fast operations?
Through vectorization (loops in C), contiguous memory, pre‑compiled universal functions, and avoiding Python‑level loops.

82. What is memory layout in NumPy (C‑order vs. F‑order)?
C‑order (row‑major): last index changes fastest. F‑order (column‑major): first index changes fastest. Use order='C' or order='F' when creating.

83. What are strides?
Strides tell NumPy how many bytes to jump in each dimension to reach the next element along that dimension. They allow views without copying.

84. How do you check if an array is C‑contiguous?
arr.flags['C_CONTIGUOUS'].

85. How do you convert an array from C to Fortran order?
np.asfortranarray(arr) or arr.copy(order='F').

86. How can you avoid unnecessary copies?
Use slices instead of fancy indexing where possible, use reshape and ravel (which return views when possible), and set copy=False in constructors.

87. What is np.memmap?
Creates a memory‑mapped array that accesses small segments of a file on disk without loading the whole file into memory. Ideal for huge arrays.

88. How do you measure performance of a NumPy operation?
Using %timeit in IPython, or Python’s time module. For micro‑benchmarking, np.show_config() reveals linked BLAS/LAPACK libraries.

89. Why is for loop over an array slow, and how to avoid it?
Python loops are slow due to interpreter overhead. Replace them with vectorized operations, np.vectorize (convenient but not super fast), or numpy.apply_along_axis.

90. How do you handle large arrays that don’t fit in memory?
Use memory‑mapped files (np.memmap), processing in chunks, distributed computing (Dask), or using out‑of‑core techniques.

91. What is np.dot vs np.matmul vs @?
np.dot for dot product of 1D arrays, matrix multiplication for 2D, and sum‑of‑products over last axis for N‑D. np.matmul strictly matrix multiplication for >2D stacks. @ is the operator for matmul.

92. How do you optimize a custom calculation that is applied to each row?
Try to reformulate using broadcasting and ufuncs. If unavoidable, use np.apply_along_axis or Numba/Cython for true speed.

I/O & Miscellaneous

93. How do you save a NumPy array to a file?
np.save('file.npy', arr) for a single array in binary .npy format. np.savez('file.npz', a=arr1, b=arr2) for multiple arrays in a compressed archive.

94. How do you load an array from a file?
np.load('file.npy') returns the array.

95. How do you read and write CSV files with NumPy?
np.loadtxt('file.csv', delimiter=',') for simple text files; np.genfromtxt('file.csv', delimiter=',') handles missing data. np.savetxt('output.csv', arr, delimiter=',').

96. How do you check the version of NumPy?
np.__version__.

97. How do you stack 1D arrays into a 2D array?
np.column_stack((a,b)) or np.stack((a,b), axis=-1).

**98. What is the difference between np.clone and np.copy?
np.copy(arr) returns an array copy. There’s no clonearr.copy() does the same.

99. How do you bin data or compute digitization?
np.digitize(x, bins) returns the index of the bin to which each value belongs.

100. You have a 2D array of pixel values and need to clip values to [0, 255] and convert to uint8. How?
clipped = np.clip(arr, 0, 255).astype(np.uint8). This preserves values in range and changes the data type.

Conclusion

You’ve reached the final question, but something has changed along the way. You’re no longer passively reading — you’re fully engaged, every array operation and broadcasting rule now crystal clear in your mind. That deep connection to the material is a quiet form of power, and it belongs entirely to you.

That crackling, positive energy you’re feeling? That’s pure enthusiasm, rising from the honest work you’ve just completed. You genuinely understand NumPy now — not just the functions, but the why behind them — and that passion will shine through every answer you give.

Now you’re genuinely excited — not just to pass an interview, but to show someone exactly how much you know. You’ve earned the right to walk in smiling, confident, and deeply proud of your preparation. Go light up that room with your energy! 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top