Topics in this subject
NumPy 4 min read Updated 4 Aug 2026

15. NumPy Interview Questions (40+)

1. Why is NumPy faster than Python lists?

1. Why is NumPy faster than Python lists? Contiguous fixed-dtype memory (cache-friendly, no per-element boxing) + vectorized operations in compiled C.

2. What is an ndarray? A multi-dimensional, homogeneous, fixed-size array — the core NumPy object, described by data buffer, dtype, shape, and strides.

3. Difference between a view and a copy? A view shares the same memory buffer (mutations propagate); a copy is independent. .base tells you if an array is a view.

4. Which operations return views vs copies? Views: basic slicing, reshape, .T, ravel. Copies: fancy indexing, boolean indexing, flatten, .copy().

5. Explain broadcasting. Aligns shapes from the right, stretches size-1 dimensions, applies ops element-wise without materializing copies.

6. flatten vs ravel? ravel returns a view when possible; flatten always returns a copy.

7. What does axis mean? The dimension collapsed by an aggregation. axis=0 = down rows (per column); axis=1 = across columns (per row).

8. np.array vs np.asarray? array copies by default; asarray avoids copying if input is already a matching ndarray.

9. What are strides? Bytes to move one step along each axis. Reshape/transpose change strides, not data.

10. How to reverse an array? a[::-1].

11. arange vs linspace? arange = spacing by step (stop exclusive); linspace = spacing by count (stop inclusive).

12. Element-wise multiply vs matrix multiply? * is element-wise; @ / np.matmul / np.dot is matrix multiplication.

13. How to find indices of the max value? np.argmax (flattened) or with axis.

14. How to count occurrences of each value? np.unique(a, return_counts=True) or np.bincount for non-negative ints.

15. What is np.where? Vectorized ternary: np.where(cond, x, y). With one arg, returns indices where condition is true.

16. Difference np.dot and np.matmul? Identical for 2-D. For >2-D, matmul broadcasts as stacks of matrices; dot does a different sum-product. For scalars, only dot works.

17. How to normalize an array to [0,1]? (a - a.min()) / (a.max() - a.min()).

18. How to standardize (z-score)? (a - a.mean()) / a.std().

19. What causes integer overflow? Fixed-width int dtypes wrap silently (e.g. int8 beyond 127). Use wider dtype.

20. How to concatenate along a new axis? np.stack.

21. hstack vs vstack? Horizontal (columns) vs vertical (rows) stacking.

22. How to handle NaN in aggregation? Use np.nansum, np.nanmean, np.nanmax, etc.

23. How to check for NaN? np.isnan(a) (NaN != NaN, so == fails).

24. How to replace NaN with a value? a[np.isnan(a)] = 0 or np.nan_to_num(a).

25. What is keepdims? Keeps reduced axes as size 1 so results broadcast back cleanly.

26. How to solve a linear system? np.linalg.solve(A, b) (not inv(A)@b).

27. When is a matrix singular? det == 0; rank < n; not invertible.

28. What is fancy indexing? Indexing with an array of integer positions; returns a copy.

29. How to select elements by condition? Boolean masking: a[a > 0].

30. What's the memory of an array? a.nbytes = a.size * a.itemsize.

31. Difference sort and argsort? sort returns sorted values; argsort returns indices that would sort.

32. How to get top-k without full sort? np.argpartition(a, -k)[-k:] in O(n).

33. What is searchsorted? Binary search for insertion positions into a sorted array — O(log n).

34. How to create an identity matrix? np.identity(n) or np.eye(n).

35. zeros vs empty? zeros initializes to 0; empty leaves garbage (faster, fill it yourself).

36. How to add a new axis? a[:, None], a[np.newaxis], or np.expand_dims.

37. What is vectorization? Expressing computation as array ops that run in C instead of Python loops.

38. Is np.vectorize fast? No — it's a convenience wrapper that still loops in Python.

39. How to compute pairwise differences? a[:, None] - a[None, :] (broadcasting).

40. How to set a random seed? rng = np.random.default_rng(42) (modern) or np.random.seed(42) (legacy).

41. rand vs randn? Uniform [0,1) vs standard normal.

42. How to shuffle an array? rng.shuffle(a) (in place) or rng.permutation(a) (copy).

43. How to flatten only certain dimensions? reshape with -1 for the merged axis.

44. What is np.clip? Limits values into [lo, hi] — great for capping outliers.

45. Why avoid np.append in loops? It reallocates the whole array each call → O(n²). Preallocate instead.