NumPy interviews rarely test whether you remember a function name. They hand you a loss that comes out ten times too big, a loop that takes a minute or a script that ran out of memory, and they watch whether you can reason about shapes, dtypes and memory. ML engineer and research loops add a second layer: write softmax, attention or a distance matrix from scratch, without a loop, and make it numerically safe.
Most slips come from three habits. Candidates treat every slice as a copy, trust broadcasting to do what they meant, and assume integer and float arithmetic behave like math on paper. NumPy 2 added new traps on top: copy=False now raises instead of copying, NEP 50 changed how scalars promote, and aliases such as np.NaN and np.float_ are gone.
The 31 questions below are grouped into five areas, each with a short note on what interviewers look for. Every solution gives the answer, the reason and the trap, with runnable code whose outputs come from a real run on NumPy 2.5, the current release as of October 2026. After the questions you will find a prep plan, a short self-check quiz and answers to common questions.
Intermediate31 questions
NumPy Interview Questions
Practice questions on the NumPy skills interviews test most: views and memory layout, broadcasting, indexing, ufuncs, numerical stability, linear algebra and performance, updated for NumPy 2.
Interviewers open with memory because NumPy's speed and many of its surprising bugs come from the same design. An array is a buffer plus a dtype, a shape and strides. Two arrays can describe the same buffer, and a write through one shows up in the other.
Hold onto one idea: basic slicing returns a view and advanced indexing returns a copy. If you can say which one you have, and confirm it with np.shares_memory, most of these questions answer themselves. Everything here targets NumPy 2.x.
Arrays, dtypes and memory layout
A helper does top = scores[:100] and then normalizes it in place with top /= top.max(). Callers later find the original scores changed. An older version used scores[idx] with an index array and had no such bug. Explain the difference and how you would check it.
EasyArrays, dtypes and memory layout
Sample Answer
scores[:100] is basic slicing, so top is a view: a new header (shape, strides, offset) over the same buffer. An in-place operation on the view writes straight into scores. scores[idx] with an integer array is advanced indexing, which always gathers into a new buffer, so writes stay local. Check with np.shares_memory(a, b), which gives an exact answer, or look at b.base, which points at the array that owns the memory. np.may_share_memory only compares address bounds and can report false positives. The fix is a deliberate .copy() when a function must not touch its input, or the out-of-place top = top / top.max().
Python
1import numpy as np
23a = np.arange(6)4b = a[::2]# basic slice: a view5c = a[[0,2,4]]# integer-array index: a copy6b[0]=997c[1]=-18print(a)# [99 1 2 3 4 5]9print(np.shares_memory(a, b), np.shares_memory(a, c))# True False10print(b.base is a, c.base isNone)# True True
A (3, 4) float64 array a has strides (32, 8). What are the strides of a.T, why is the transpose free, and why does a.T.reshape(-1) allocate a new array when a.reshape(-1) does not?
MediumArrays, dtypes and memory layout
Sample Answer
Strides are the bytes to step along each axis. In C (row-major) order, the next column is 8 bytes away and the next row 4 × 8 = 32 bytes. a.T only swaps them to (8, 32): no data moves, so it costs O(1) and the result is F-contiguous. reshape returns a view only when the new shape can be described by strides over the existing buffer. Flattening a.T in C order means walking the buffer column by column, which no single stride can do, so NumPy copies. reshape(-1, order="F") follows the memory and stays a view. In NumPy 2.x, reshape(..., copy=False) raises instead of copying, which is how you assert a view. Assigning to arr.shape is deprecated as of 2.5.
Python
1import numpy as np
23a = np.arange(12, dtype=np.float64).reshape(3,4)4print(a.strides, a.T.strides)# (32, 8) (8, 32)5print(a.T.flags.c_contiguous, a.T.flags.f_contiguous)# False True6print(np.shares_memory(a, a.T.reshape(-1)))# False: copied7print(np.shares_memory(a, a.T.reshape(-1, order="F")))# True: a view8try:9 a.T.reshape(-1, copy=False)10except ValueError as e:11print(e)# Unable to avoid creating a copy while reshaping.
Code written for NumPy 1.x calls np.array(batch, copy=False) to avoid copies. After the upgrade to NumPy 2 it raises ValueError for some inputs. What changed, and what should the code say instead?
MediumArrays, dtypes and memory layout
Sample Answer
In NumPy 1.x, copy=False meant "copy only if needed". NumPy 2.0 changed it to "never copy": if the input is a Python list, needs a dtype conversion or has the wrong memory order, NumPy raises ValueError: Unable to avoid copy while creating an array as requested. The old meaning is now copy=None, and the migration guide recommends np.asarray(batch), which returns the very same object when nothing needs converting. Keep copy=False only where you need the guarantee, for example when you will write results back into a caller's buffer. Note that astype(dtype, copy=False) did not change: it returns the input when the dtype already matches and quietly copies otherwise.
Python
1import numpy as np
23try:4 np.array([1.0,2.0,3.0], copy=False)# a list always needs a copy5except ValueError as e:6print(str(e).splitlines()[0])7x = np.ones(3, dtype=np.float32)8print(np.asarray(x)is x)# True: no copy needed9print(np.asarray(x, dtype=np.float64)is x)# False: converted copy10print(x.astype(np.float32, copy=False)is x)# True
A loader reads an 800 MB array, keeps first_row = big[0] for logging and runs del big. Process memory never drops. Why, and how do you confirm and fix it?
HardArrays, dtypes and memory layout
Sample Answer
big[0] is a view, and a view holds a reference to the array that owns its buffer. While first_row is alive the whole 800 MB stays allocated, even though the view exposes only 80 KB. del big removes a name, not the buffer. first_row.base shows what is being kept alive and first_row.base.nbytes how much. The fix is big[0].copy(), which owns 80 KB of its own and drops the reference. The same leak appears with slices stored in caches, windows from sliding_window_view kept in a list, and memory-mapped files that stay open because a small view escaped. Copy at the point where a small piece outlives a large array.
Python
1import numpy as np
23big = np.zeros((10_000,10_000))# 800 MB4row = big[0]# a view onto 80 KB of it5print(row.base is big, row.base.nbytes //10**6)# True 8006row = big[0].copy()# owns its own 80 KB7del big # now the 800 MB can be freed8print(row.base, row.nbytes)# None 80000
A colleague's script fails on NumPy 2 with AttributeError: np.NaN was removed in the NumPy 2.0 release. What else is likely to break, and how do you migrate it?
EasyArrays, dtypes and memory layout
Sample Answer
NumPy 2.0 cleaned up its main namespace. Removed aliases include np.NaN, np.Inf and np.PINF (use np.nan, np.inf), np.float_ (np.float64), np.complex_ (np.complex128), np.string_ (np.bytes_), np.unicode_ (np.str_), np.round_ (np.round), np.alltrue and np.sometrue (np.all, np.any) and np.product (np.prod). Functions deprecated in 2.0 have since gone too: np.in1d and np.trapz were removed in 2.4 (use np.isin, np.trapezoid) and np.row_stack is gone in 2.5 (np.vstack). The Ruff linter's NPY201 rule rewrites most of these automatically. Also check code that assumed a 32-bit default integer on Windows, which is 64-bit since 2.0.
Python
1import numpy as np
23print(np.__version__)# 2.5.44for name in["NaN","float_","in1d","row_stack"]:5try:6getattr(np, name)7except AttributeError as e:8print(name,"->",str(e)[:58])9print(np.isin([1,2],[2]), np.trapezoid([1,2,3]))# [False True] 4.0
Training data is float32. After someone writes X = X * np.float64(scale), memory use doubles. Elsewhere pixels + 300 on a uint8 array raises OverflowError while pixels + 100 silently wraps. Explain NumPy 2's promotion rules.
MediumArrays, dtypes and memory layout
Sample Answer
NumPy 2 follows NEP 50. Python scalars (int, float, complex) are weak: they adopt the array's dtype. NumPy scalars are strong and promote like arrays. So X * 2.0 stays float32, but X * np.float64(2.0) makes the whole result float64. Values returned by NumPy, such as np.mean of float64 data, are np.float64, so this sneaks in easily. A weak Python int must fit the array's dtype: uint8 + 300 raises OverflowError: Python integer 300 out of bounds for uint8, while uint8 + 100 computes in uint8 and wraps 200 to 44, with no warning for arrays. NumPy 1.x used value-based casting instead, so np.float32(3) + 3.0 used to be float64 and is now float32.
Python
1import numpy as np
23x = np.ones(3, dtype=np.float32)4print((x *2.0).dtype,(x * np.float64(2.0)).dtype)# float32 float645print((np.float32(3)+3.0).dtype)# float326u = np.array([200], dtype=np.uint8)7print(u +100)# [44], wrapped8try:9 u +30010except OverflowError as e:11print(e)# Python integer 300 out of bounds for uint8
Broadcasting lets NumPy combine arrays of different shapes without copying data. It is also the most common source of code that runs, returns an array, and computes the wrong thing. Interviewers like it because one misplaced axis turns a vector of errors into a matrix and nobody gets an exception.
The rule fits in a sentence: align shapes from the right, and each pair of sizes must be equal or contain a 1. Say the result shape out loud before you run a line, and use keepdims=True on any reduction you plan to broadcast back.
Broadcasting rules and traps
You center a (n, d) matrix by columns with X - X.mean(axis=0) and it works. Centering by rows with X - X.mean(axis=1) raises. Explain with the broadcasting rules, then give the result shape of combining (8, 1, 6, 1) with (7, 1, 5).
EasyBroadcasting rules and traps
Sample Answer
NumPy compares shapes starting from the last dimension. Two dimensions are compatible when they are equal or one of them is 1, and missing leading dimensions count as 1. X.mean(axis=0) has shape (d,), which lines up with the last axis of (n, d). X.mean(axis=1) has shape (n,), which also lines up with the last axis, so it fails unless n equals d. If n does equal d it runs and subtracts the wrong means. keepdims=True keeps the reduced axis as (n, 1), which broadcasts across columns. For (8, 1, 6, 1) and (7, 1, 5): right-align to (8, 1, 6, 1) and (1, 7, 1, 5), take the larger size in each position, (8, 7, 6, 5). np.broadcast_shapes checks this without allocating anything.
Python
1import numpy as np
23X = np.arange(12.0).reshape(4,3)4print((X - X.mean(axis=0)).shape)# (4, 3)5try:6 X - X.mean(axis=1)# (4, 3) vs (4,)7except ValueError as e:8print(e)9print((X - X.mean(axis=1, keepdims=True)).shape)# (4, 1) -> (4, 3)10print(np.broadcast_shapes((8,1,6,1),(7,1,5)))# (8, 7, 6, 5)
An evaluation script reports MSE 1.42 for a model whose predictions are all within 0.2 of the target. y_true has shape (n,) and y_pred comes from model.predict with shape (n, 1). Find the bug.
MediumBroadcasting rules and traps
Sample Answer
(n, 1) minus (n,) broadcasts to (n, n): every prediction is compared with every target, and the mean of that matrix means nothing. There is no error because the shapes are compatible. With three points the true MSE is 0.02, and the broadcast version reports 1.42. Make the shapes match before the arithmetic, with y_pred.ravel() or y_pred[:, 0], then assert it: assert y_pred.shape == y_true.shape. This is the most common broadcasting bug in ML code, and it hides in losses, metrics and residual plots. Library metrics validate their inputs and catch it. A hand-written np.mean((a - b) ** 2) never will.
Python
1import numpy as np
23y_true = np.array([1.0,2.0,3.0])# (3,)4y_pred = np.array([[1.1],[1.9],[3.2]])# (3, 1), as predict returns it5err = y_pred - y_true
6print(err.shape)# (3, 3)7print(np.mean(err **2))# 1.42..., wrong8print(np.mean((y_pred.ravel()- y_true)**2))# 0.02..., right
Compute all pairwise Euclidean distances between 2,000 query vectors and 3,000 database vectors of dimension 128. The broadcasting one-liner runs out of memory on a small machine. Why, and what do you write instead?
MediumBroadcasting rules and traps
Sample Answer
X[:, None, :] - Y[None, :, :] materializes a (2000, 3000, 128) float64 array, 5.7 GiB, and squaring it creates a second one before sum collapses the last axis. Expand the square instead: the squared distance is $\|x\|^2 + \|y\|^2 - 2\,x \cdot y$. The cross term for all pairs is a single matrix product, X @ Y.T, which runs on BLAS and only allocates the (2000, 3000) result. Rounding can push near-zero distances slightly below zero (two almost identical rows gave -5.7e-14 in our run), and np.sqrt of a negative is NaN, so clip with np.maximum(sq, 0) first. If even the result is too large, process the queries in chunks.
Python
1import numpy as np
23rng = np.random.default_rng(0)4X, Y = rng.normal(size=(2000,128)), rng.normal(size=(3000,128))5print(2000*3000*128*8/2**30)# 5.72 GiB for X[:, None] - Y[None]6sq =(X**2).sum(1)[:,None]+(Y**2).sum(1)[None,:]-2* X @ Y.T
7D = np.sqrt(np.maximum(sq,0))# clip tiny negatives from rounding8ref = np.linalg.norm(X[:5,None]- Y[None,:5], axis=-1)9print(np.abs(D[:5,:5]- ref).max())# ~1e-14
In grouped-query attention, 8 query heads share 2 key heads. A teammate lines them up with np.repeat(k, 4, axis=0). Compute the attention scores without materializing the repeated keys.
HardBroadcasting rules and traps
Sample Answer
np.repeat copies each key head four times, which for a long KV cache is a large allocation holding no new information. Reshape the queries so the group gets its own axis: q.reshape(G, H // G, T, d) is a view of shape (2, 4, T, d). Give the keys a size-1 axis in the same position, k[:, None], so they are (2, 1, T, d), and swap their last two axes for the product. matmul broadcasts batch dimensions, so all four query heads in a group read the same key head with no copy. Reshape the (2, 4, T, T) scores back to (8, T, T). The trap is head order: this layout means query head h uses key head h // 4, the convention Llama-style checkpoints follow.
Why does a += b raise when a + b works, and why does writing into the result of np.broadcast_to fail?
MediumBroadcasting rules and traps
Sample Answer
a + b allocates a new output with the broadcast shape. a += b writes into a, so the broadcast shape must equal the shape of a. With a of shape (3,) and b of shape (2, 3), NumPy raises non-broadcastable output operand with shape (3,) doesn't match the broadcast shape (2,3). In-place operations can broadcast the right-hand side, never the target. np.broadcast_to returns a view with stride 0 along the new axes, so 1,000 rows cost no memory, but every row is the same bytes. Writing one cell would change all of them, so NumPy marks the view read-only and raises assignment destination is read-only. Call .copy() when you need a writable full-size array.
Python
1import numpy as np
23a = np.zeros(3)4try:5 a += np.ones((2,3))6except ValueError as e:7print(e)8b = np.broadcast_to(np.arange(3),(1000,3))9print(b.strides, b.flags.writeable)# (0, 8) False10try:11 b[0,0]=512except ValueError as e:13print(e)# assignment destination is read-only
You normalize image batches stored as (N, C, H, W) with a per-channel mean of shape (C,). It raises on 32 x 32 images, yet a unit test with 3 x 3 RGB images passed. What is happening?
MediumBroadcasting rules and traps
Sample Answer
A (C,) array aligns with the last axis, W, not with C. With W = 32 and C = 3 the shapes are incompatible and NumPy raises. In the test, W happened to equal C, so the subtraction broadcast along the width and each column received a different channel's mean: a wrong answer with no error, which is why the test passed. Put the channel axis where it belongs, mean[:, None, None] with shape (3, 1, 1), or compute the statistics with keepdims=True to get (1, 3, 1, 1) directly. In a channels-last (N, H, W, C) layout a plain (C,) array is correct, which is why code copied from such a pipeline breaks here. Test shape-sensitive code with a different size on every axis.
Python
1import numpy as np
23x = np.random.default_rng(0).normal(size=(16,3,32,32))# N, C, H, W4mean = x.mean(axis=(0,2,3))# (3,)5try:6 x - mean
7except ValueError as e:8print(e)9z = x - mean[:,None,None]# (3, 1, 1)10print(np.allclose(z.mean(axis=(0,2,3)),0))# True11print(x.mean(axis=(0,2,3), keepdims=True).shape)# (1, 3, 1, 1)
Indexing questions check whether you know what comes back: a view or a copy, which shape, and whether an assignment actually reaches the original array. NumPy has two systems that look alike on the page. Basic indexing uses integers, slices, None and .... Advanced indexing uses integer arrays and boolean masks.
The fact to remember: advanced indexing copies when you read, and writes through only when it is the whole target of one assignment. Integer arrays used together are broadcast and paired element by element, never combined as a grid unless you ask with np.ix_.
Indexing: basic, fancy and boolean
a[a < 0][0] = 0 is meant to clear the first negative value. Nothing changes and no error is raised. Why, and what is the right version?
EasyIndexing: basic, fancy and boolean
Sample Answer
Python evaluates a[a < 0] first. A boolean mask is advanced indexing, so this returns a new array holding copies of the negatives, and [0] = 0 writes into that temporary, which is then thrown away. An assignment reaches a only when the advanced index is the whole target of one indexing call: a[a < 0] = 0 clears every negative. To clear only the first, convert the mask to positions and index once: a[np.flatnonzero(a < 0)[0]] = 0. NumPy gives no warning here. Chains of basic slices such as a[1:][0] = 0 do work, because every step is a view, which is what makes the rule easy to forget.
Python
1import numpy as np
23a = np.array([3,-1,4,-2,5])4a[a <0][0]=0# writes into a temporary copy5print(a)# [ 3 -1 4 -2 5]6a[a <0]=0# one indexing call on a itself7print(a)# [3 0 4 0 5]8b = np.array([3,-1,4,-2,5])9b[np.flatnonzero(b <0)[0]]=0# first negative only10print(b)# [ 3 0 4 -2 5]
From a 4 x 4 matrix you want the 2 x 2 block at rows [0, 2] and columns [1, 3]. A[[0, 2], [1, 3]] returns two numbers. Explain, fix it, and show how to assign to that block.
MediumIndexing: basic, fancy and boolean
Sample Answer
Integer arrays used together are broadcast against each other and paired element by element, so A[[0, 2], [1, 3]] picks the cells (0, 1) and (2, 3), giving [1, 11]. To get every combination of rows and columns, the index arrays must have shapes (2, 1) and (1, 2), which is what np.ix_(rows, cols) builds. A[np.ix_(rows, cols)] returns the block, and because it is a single indexing call it also works as an assignment target: A[np.ix_(rows, cols)] = 0 zeroes those four cells. A[rows][:, cols] reads the same block, but you cannot assign through it, since the first step already made a copy.
You have predicted probabilities P of shape (N, K) and integer labels y of shape (N,). Write the mean cross-entropy without a loop. A candidate writes P[:, y]. What does that return?
MediumIndexing: basic, fancy and boolean
Sample Answer
P[:, y] keeps every row and, for each one, takes all N columns listed in y, so the result is (N, N). Averaging its log gives a wrong number and costs N squared memory. You need one entry per row: pair row i with column y[i] by passing two integer arrays, P[np.arange(N), y], which has shape (N,). The loss is -np.log(P[np.arange(N), y]).mean(). To select several columns per row, such as top-k indices from argsort, use np.take_along_axis(P, idx, axis=1), which does the pairing for you. In training code, index into log-probabilities computed from logits instead of taking the log of P, so a probability that underflowed to 0 cannot produce an infinite loss.
You count token frequencies with counts[ids] += 1, and the same pattern accumulates embedding gradients with dE[ids] += grad. The counts come out too low. Explain and fix both.
HardIndexing: basic, fancy and boolean
Sample Answer
counts[ids] += 1 runs as counts[ids] = counts[ids] + 1. The read gathers one value per index into a copy, the add happens on the copy, and the write assigns back. A repeated index is assigned several times with the same value, so a token that appears three times is counted once. The NumPy indexing guide calls this out. np.add.at(counts, ids, 1) is the unbuffered version: it applies the operation once per index, duplicates included. For plain counts, np.bincount(ids, minlength=V) is faster. For embedding gradients, np.add.at(dE, ids, grad) sums the rows of grad into the right rows of dE, the same scatter-add an autograd engine performs. Every ufunc has .at, so np.maximum.at gives a per-bucket maximum.
x has shape (2, 3, 4). Why is x[0, :, [0, 1]].shape equal to (2, 3) and not (3, 2)?
HardIndexing: basic, fancy and boolean
Sample Answer
When an integer appears in the same index as an array, NumPy treats it as part of the advanced index. Here the two advanced indices, 0 and [0, 1], are separated by a slice. NumPy then has no natural place for the broadcast index dimensions, so the documented rule applies: they come first, followed by the sliced dimensions. The pair broadcasts to shape (2,), the slice keeps 3, giving (2, 3). When the advanced indices are adjacent, as in x[:, [0, 1], [0, 1]], their dimensions replace the indexed ones in place, giving (2, 2). To get (3, 2), index in two steps, x[0][:, [0, 1]], where the first step is a basic view.
Python
1import numpy as np
23x = np.zeros((2,3,4))4print(x[0,:,[0,1]].shape)# (2, 3): advanced dims go first5print(x[0][:,[0,1]].shape)# (3, 2): two steps6print(x[:,[0,1],[0,1]].shape)# (2, 2): adjacent, stays in place7y = np.zeros((5,6,7,8))8print(y[:,[0,1],:,[0,1]].shape)# (2, 5, 7)
A feature matrix has NaNs scattered through it. Drop the rows with any NaN, list where the NaNs are, and replace them with 0 in a copy. Also, why does X[X > 2] come back 1-D?
EasyIndexing: basic, fancy and boolean
Sample Answer
A boolean mask with the same shape as X selects individual elements, and those no longer form a grid, so the result is flattened in C order. To keep whole rows, reduce the mask to one boolean per row first: X[~np.isnan(X).any(axis=1)]. np.argwhere(np.isnan(X)) returns (row, column) pairs, and np.nonzero returns the same positions as a tuple of arrays ready for indexing. np.where(np.isnan(X), 0.0, X) builds a full-shape copy with the replacements, while X[np.isnan(X)] = 0 does the same in place. Testing with X == np.nan never works, because NaN compares unequal to everything, itself included.
Performance questions test whether you can move work out of the Python interpreter and into compiled loops without running out of memory on the way. A ufunc processes a whole array in C, while a Python loop pays interpreter overhead on every element. Interviewers usually give you a loop to remove, then ask what your version costs in memory.
The idea that matters most: every expression allocates a full-size result unless you tell it where to write. Ufunc methods (reduce, accumulate, reduceat, at), the out= and where= keywords, and einsum with optimize=True cover most of what a loop would do.
Vectorization, ufuncs and performance
A teammate wraps a Python function in np.vectorize and says the code is now vectorized. Is it? Rewrite a three-tier bucketing function properly.
EasyVectorization, ufuncs and performance
Sample Answer
No. The NumPy docs say np.vectorize is provided for convenience, not performance, and that its implementation is essentially a for loop. It calls your Python function once per element and adds broadcasting and output handling on top. Real vectorization expresses the logic as whole-array operations. For tiers, np.select([x > 1, x > 0], [2.0, 1.0], default=0.0) checks conditions in order like an if/elif chain, np.where handles two branches, and np.digitize or np.searchsorted handle many sorted thresholds. On one million values in our run, np.vectorize took 61 ms and np.select 6 ms. Timings vary by machine, so measure with timeit on realistic sizes before claiming a speedup.
Python
1import numpy as np, timeit
23x = np.random.default_rng(0).normal(size=1_000_000)4deftier(v):5return2.0if v >1else(1.0if v >0else0.0)6slow = np.vectorize(tier)7fast =lambda: np.select([x >1, x >0],[2.0,1.0], default=0.0)8print(np.array_equal(slow(x), fast()))# True9t1 = timeit.timeit(lambda: slow(x), number=3)/310t2 = timeit.timeit(fast, number=3)/311print(f"vectorize {t1*1e3:.0f} ms, select {t2*1e3:.1f} ms")
To avoid dividing by zero, someone writes np.divide(num, den, where=den != 0). Tests pass, but production output contains random numbers. Why?
MediumVectorization, ufuncs and performance
Sample Answer
where= tells the ufunc which positions to compute. Where the mask is False the output is left alone, and without out= the output is a freshly allocated, uninitialized buffer, so those slots hold whatever bytes were already in memory. In small tests the allocator often returns zeroed or recycled memory, which hides the bug. Pass the output buffer yourself: np.divide(num, den, out=np.zeros_like(num), where=den != 0). Recent releases warn about the pattern (NumPy 2.5.4 prints UserWarning: 'where' used without 'out', expect uninitialized memory in output). np.where(den != 0, num / den, 0.0) is also correct, but it computes every division first, so wrap it in np.errstate(divide="ignore", invalid="ignore").
Python
1import numpy as np
23num = np.array([1.0,2.0,3.0,4.0])4den = np.array([2.0,0.0,4.0,0.0])5safe = np.divide(num, den, out=np.zeros_like(num), where=den !=0)6print(safe)# [0.5 0. 0.75 0. ]7with np.errstate(divide="ignore", invalid="ignore"):8print(np.where(den !=0, num / den,0.0))# same, computes all
Given a price series, compute the maximum drawdown (the largest drop from a running peak) without a Python loop. Then sum values by segment, given the start index of each segment.
MediumVectorization, ufuncs and performance
Sample Answer
Every binary ufunc has methods that apply it along an axis. np.maximum.accumulate(prices) returns the running peak, so prices / peak - 1 is the drawdown at each point and its minimum is the maximum drawdown: -0.3846 here, the fall from 130 to 80. reduce collapses an axis (np.sum is built on np.add.reduce), accumulate keeps every intermediate result (np.cumsum is np.add.accumulate), and reduceat reduces between start indices, so np.add.reduceat(prices, [0, 3, 5]) gives segment sums of 310, 225 and 190. The reduceat trap: if an index is not smaller than the next one, that slot returns the single element at the index, not an empty sum.
A feature transform Z = np.log1p(np.abs(X - mu)) / sd on a large float32 array runs out of memory, though X itself fits easily. Where does the memory go, and how do you cut the peak?
MediumVectorization, ufuncs and performance
Sample Answer
Every ufunc call returns a new full-size array, and the previous temporary is freed only after the next call has allocated its own result. So two full-size temporaries are alive at once. Measured with tracemalloc on a 160 MB array, the chained expression peaked at 320 MB on top of X. Writing into one buffer with out= keeps it to a single allocation: subtract into Z, then np.abs(Z, out=Z), np.log1p(Z, out=Z) and Z /= sd, which peaked at 160 MB. Check dtypes as well. Under NEP 50 X - 0.5 stays float32, but X - np.float64(0.5) turns the whole result into float64 and doubles it. For inputs larger than memory, process in chunks.
Python
1import numpy as np, tracemalloc
23X = np.random.default_rng(0).random((10_000_000,4), dtype=np.float32)# 160 MB4mu, sd = X.mean(0), X.std(0)5tracemalloc.start()6Z = np.log1p(np.abs(X - mu))/ sd
7print(tracemalloc.get_traced_memory()[1]//10**6)# 3208del Z; tracemalloc.reset_peak()9Z = np.subtract(X, mu)# the only allocation10np.abs(Z, out=Z); np.log1p(Z, out=Z); Z /= sd
11print(tracemalloc.get_traced_memory()[1]//10**6)# 160
np.einsum("ij,jk,kl->il", A, B, C) with shapes (300, 400), (400, 500) and (500, 20) takes most of a second, while A @ (B @ C) is instant. Explain and fix it.
HardVectorization, ufuncs and performance
Sample Answer
np.einsum defaults to optimize=False, which evaluates the expression as one nested loop over all four indices: 300 × 400 × 500 × 20 terms, a naive count of 3.6e9 FLOPs according to np.einsum_path. With optimize=True (or "optimal"), NumPy first picks a contraction order, here B with C, then A, and runs pairwise contractions that can use BLAS. The FLOP count drops to 1.28e7 and the scaling from 4 indices to 3. In our runs that was about 0.72 s versus under 0.3 ms. Pass optimize=True whenever there are three or more operands, and print the plan with np.einsum_path when a contraction is slow. For a plain two-operand product, @ is clearer and at least as fast.
Python
1import numpy as np, timeit
23rng = np.random.default_rng(0)4A, B, C = rng.random((300,400)), rng.random((400,500)), rng.random((500,20))5path, info = np.einsum_path("ij,jk,kl->il", A, B, C, optimize="optimal")6print(path)# ['einsum_path', (1, 2), (0, 1)]7print([l.strip()for l in info.splitlines()if"FLOP"in l])# 3.600e+09, 1.280e+078for opt in(False,True):9 t = timeit.timeit(lambda: np.einsum("ij,jk,kl->il", A, B, C, optimize=opt), number=1)10print(opt,f"{t*1e3:.2f} ms")
A rolling mean over 10 million float32 sensor readings (values near 1,000) uses the cumulative-sum trick (c[k:] - c[:-k]) / k. Near the end of the series the means are off by as much as 40. Why, and what are the fixes?
HardVectorization, ufuncs and performance
Sample Answer
The running total near the end is about 1e10. At that size, neighbouring float32 values are 1,024 apart (np.spacing(np.float32(1e10))), so each reading near 1,000 is rounded to a step of 1,024 when it is added, and the difference of two totals inherits that error. Accumulating in float64 with np.cumsum(y, dtype=np.float64) removes it here. Alternatively, skip the running total: sliding_window_view(y, k) returns an (n - k + 1, k) view without copying, and .mean(axis=1, dtype=np.float64) costs O(nk) but each window stays small. np.sum itself uses pairwise summation and loses far less precision, but cumsum has to add strictly left to right.
This group covers what goes wrong when the math is right and the arithmetic is not: rounding, overflow, NaN, ill-conditioned systems and random streams that are less independent than they look. ML and quant interviews lean on it because training losses and pricing models break here first.
One idea runs through all of it. Floats and fixed-width integers are finite: exp overflows past about 709.78 in float64 and 88.72 in float32, uint8 wraps at 256, and a condition number of 1e13 costs about 13 of float64's roughly 16 digits. Know the limits and reach for the stable formulation first.
Numerics, linear algebra and random numbers
A test asserts np.isclose(result, expected) and passes, yet the two values differ by a factor of two. Both are around 1e-9. What happened, and how should floats be compared?
EasyNumerics, linear algebra and random numbers
Sample Answer
np.isclose(a, b) checks abs(a - b) <= atol + rtol * abs(b) with defaults rtol=1e-05 and atol=1e-08. Near 1e-9 the absolute tolerance alone is ten times bigger than the numbers, so np.isclose(1e-9, 2e-9) is True. The docs warn that the default atol is not appropriate when the reference value is smaller than one. Choose tolerances for the scale: atol=0 for a purely relative test, or an atol tied to your noise floor. Two more traps. The test is not symmetric, because b is the reference: np.isclose(1, 1.00001, atol=0) is True and the swapped call is False. And NaN is never close to NaN unless you pass equal_nan=True. Never compare floats with ==.
An image-diff job computes np.abs(frame_a - frame_b) on uint8 frames and flags nearly every pixel as changed. Explain the bug, and say what frame_a.sum() returns.
MediumNumerics, linear algebra and random numbers
Sample Answer
uint8 arithmetic wraps modulo 256. 10 - 20 gives 246, not -10, and np.abs of an unsigned value changes nothing, so small differences look huge. Array operations wrap silently; only operations on NumPy scalars warn. Cast to a signed type wide enough for the result before subtracting: np.abs(a.astype(np.int16) - b) gives 10. Addition has the same issue: 200 + 100 becomes 44. Reductions behave differently. np.sum and np.cumsum on small integer types accumulate in the platform-sized integer (uint64 for unsigned input on a 64-bit machine), so a.sum() is 260, and mean returns float64. Wider types still wrap: an int32 holding 2**31 - 1 plus 1 becomes -2147483648 with no error.
A daily metric array has a few NaNs from failed jobs. The dashboard shows NaN for the mean and the max, and argmax returns the date of a failed job. Explain NumPy's NaN behavior and the right tools.
MediumNumerics, linear algebra and random numbers
Sample Answer
NaN propagates through arithmetic and through np.mean, np.sum and np.max, so one missing value poisons the aggregate. np.argmax returns the index of the first NaN, because NumPy treats NaN as the maximum when NaNs are present. Use the NaN-aware versions, np.nanmean, np.nanmax, np.nanargmax and np.nanpercentile, which skip missing values, or np.fmax and np.fmin for element-wise comparisons that prefer the number. An all-NaN slice still returns NaN, with RuntimeWarning: Mean of empty slice. Sorting places NaN last, and np.unique collapses all NaNs into one by default (equal_nan=True). Count missing values with np.isnan(x).sum(), never with x == np.nan.
Python
1import numpy as np
23x = np.array([1.0, np.nan,3.0])4print(np.mean(x), np.nanmean(x))# nan 2.05print(np.max(x), np.argmax(x))# nan 16print(np.nanmax(x), np.nanargmax(x))# 3.0 27print(np.fmax(x,2.0))# [2. 2. 3.]8print(np.sort([3.0, np.nan,1.0]))# [ 1. 3. nan]9print(np.unique([np.nan, np.nan,1.0]))# [ 1. nan]
Implement log-softmax for a batch of logits that can reach 1,000. Show why the naive version fails, and explain how a row of attention scores that is entirely masked can still produce NaN.
HardNumerics, linear algebra and random numbers
Sample Answer
np.exp(1000.0) overflows to inf in float64 (the limit is about 709.78, and about 88.72 in float32), and inf divided by inf is NaN. Softmax does not change when every logit in a row shifts by the same amount, so subtract the row max first: every exponent is then at most 0 and the largest term is exactly 1. Never take np.log of the softmax output: tiny probabilities underflow to 0 and give -inf. Compute z - logsumexp(z), where logsumexp is m + log(sum(exp(z - m))). NumPy has no logsumexp function, but np.logaddexp.reduce computes the same value. The remaining trap is a row where every logit is -inf, such as a query with every key masked: the max is -inf, z - m is NaN, and you must decide what that row returns.
Python
1import numpy as np
23deflog_softmax(z, axis=-1):4 m = z.max(axis=axis, keepdims=True)5return z -(m + np.log(np.exp(z - m).sum(axis=axis, keepdims=True)))67z = np.array([[1000.0,999.0,0.0],[2.0,1.0,0.1]])8with np.errstate(over="ignore", invalid="ignore"):9print(np.exp(z[0])/ np.exp(z[0]).sum())# [nan nan 0.]10print(np.exp(log_softmax(z)).round(4))# [[0.7311 0.2689 0.] ...]11print(log_softmax(z)[0,2])# -1000.313..., finite12print(np.logaddexp.reduce(z[0]))# 1000.313...
You have a batch of matrices A of shape (8, 3, 4) and B of shape (8, 4, 5). Someone uses np.dot(A, B) and gets a (8, 3, 8, 5) array. Explain how dot, matmul and einsum treat stacked inputs.
MediumNumerics, linear algebra and random numbers
Sample Answer
np.matmul, the @ operator, treats the last two axes as matrices and broadcasts the leading axes as a batch, so A @ B is (8, 3, 5). np.dot on N-d arrays is a sum product over the last axis of the first argument and the second-to-last axis of the second, so it pairs every batch of A with every batch of B: (8, 3, 8, 5), eight times the work, with the answer you wanted sitting on the diagonal blocks. np.einsum("bij,bjk->bik", A, B) names every axis, which keeps batched and transposed products readable, such as attention scores "bhqd,bhkd->bhqk". Two more differences: matmul rejects scalar operands (use *), and dot of two 1-D arrays is the inner product.
Python
1import numpy as np
23rng = np.random.default_rng(0)4A, B = rng.random((8,3,4)), rng.random((8,4,5))5print(np.matmul(A, B).shape)# (8, 3, 5)6print(np.dot(A, B).shape)# (8, 3, 8, 5)7print(np.einsum("bij,bjk->bik", A, B).shape)# (8, 3, 5)8print(np.allclose(np.dot(A, B)[2,:,2],(A @ B)[2]))# True: diagonal block9try:10 A @ 2.011except ValueError as e:12print(str(e)[:40])# matmul: Input operand 1 ...
A colleague solves a linear system with np.linalg.inv(A) @ b. Why is np.linalg.solve(A, b) preferred, and what changed about the shape of b in NumPy 2?
HardNumerics, linear algebra and random numbers
Sample Answer
solve factorizes A once with LU (LAPACK gesv) and back-substitutes, which takes fewer operations than forming the inverse and is more accurate. On a 10 x 10 Hilbert matrix with condition number 1.6e13, solve left a residual of 5.2e-16 and the inverse route 5.1e-4. Both lose digits in x itself, since conditioning limits what any method can recover. For non-square or rank-deficient systems use np.linalg.lstsq. NumPy 2.0 changed how b is read: it counts as a vector only when it is exactly 1-D, and anything else is a stack of (M, K) matrices. A batch of vectors with shape (batch, M) now raises a core-dimension mismatch and needs a trailing axis: solve(A, b[..., None])[..., 0].
Python
1import numpy as np
23n =104H =1.0/(np.arange(n)[:,None]+ np.arange(n)+1)# Hilbert matrix5b = H @ np.ones(n)6print(f"{np.linalg.cond(H):.1e}")# 1.6e+137for x in(np.linalg.solve(H, b), np.linalg.inv(H) @ b):8print(f"{np.linalg.norm(H @ x - b):.1e}")# 5.2e-16, then 5.1e-049Hs, bs = np.stack([H, H]), np.stack([b, b])# (2, n, n), (2, n)10print(np.linalg.solve(Hs, bs[...,None])[...,0].shape)# (2, 10)
A Monte Carlo simulation calls np.random.seed(0) and runs 8 worker processes. Results are not reproducible, and some workers produce identical paths. How would you set up randomness with the current API?
MediumNumerics, linear algebra and random numbers
Sample Answer
np.random.seed sets hidden global state in the legacy RandomState API. Any library call can advance it, and forked processes inherit identical copies. The current API is explicit: create a Generator with rng = np.random.default_rng(seed) (PCG64 underneath) and pass it to every function that draws numbers. For parallel work, derive independent child streams with rng.spawn(8) or np.random.SeedSequence(seed).spawn(8), rather than ad hoc seeds such as seed + i. Method names differ from the legacy functions: rng.integers(0, 10) excludes 10 by default, and rng.random replaces rand. One seed reproduces a stream within a NumPy version, but Generator streams are not guaranteed to stay identical across versions, unlike the frozen RandomState.
Python
1import numpy as np
23rng = np.random.default_rng(42)4print(type(rng.bit_generator).__name__)# PCG645print(rng.integers(0,10, size=5))# [0 7 6 4 4]: 10 excluded6workers = rng.spawn(3)# independent child streams7print([int(w.integers(0,100))for w in workers])# [49, 7, 70]8same =[np.random.default_rng(s)for s in np.random.SeedSequence(42).spawn(3)]9print([int(g.integers(0,100))for g in same])# [49, 7, 70]
Before running a line, write its result shape as a comment and check it. This one habit catches the (n, 1) versus (n,) bug, the wrong reduction axis and the channel mix-up. Drill it in Tensors, shapes and broadcasting, then in Batch and layer norm, where batch norm and layer norm differ only in the axis you reduce over.
Write the stable versions from memory
Be able to write softmax, log-softmax via logsumexp and binary cross-entropy from logits without looking anything up, and explain why each subtracts a max. Loss functions has you write all three in NumPy, and FlashAttention extends the same trick to the online softmax that tiled attention depends on.
Implement attention in plain NumPy
Masked multi-head attention is the most common "write it without a loop" prompt for ML roles, and it exercises batched @, swapaxes, np.triu masks and broadcasting all at once. Work through Implement attention, then MQA, GQA and MLA, where several query heads share one key head and the grouping is a reshape.
Know where each dtype breaks
Memorize a few limits: float32 has about 7 significant digits, float16 overflows above 65,504, and uint8 wraps at 256. Then practise spotting where a computation needs a wider type. RMSNorm and where the norms go shows why every implementation upcasts to float32 before squaring, and RoPE in depth builds its angle table with np.outer and broadcasting.
Check every vectorized function against a loop
When you replace a loop, keep the slow version as a reference and compare with np.allclose on random inputs, including odd sizes and edge cases. For derivatives, compare against a central difference. A neuron and a forward pass writes a batched layer as one matrix product, and Activations and vanishing gradients checks NumPy derivatives against numerical slopes.
How Ready Are You for NumPy Interviews?
1 / 6
Memory
b = a[::2]; b[0] = 99. What happens to a?
Frequently Asked Questions
Is NumPy asked in data science and machine learning interviews?
Yes, often, though rarely as a standalone round. ML engineer and research loops ask you to implement softmax, attention, k-means or a distance matrix in NumPy without loops. Data science rounds use it inside pandas and model code, and quant roles use it for simulations and vectorized backtests. Interviewers watch for correct shapes, awareness of views and copies, and numerically stable formulas more than for memorized function names.
Which NumPy version should I prepare for?
Prepare for NumPy 2.x. Version 2.0 shipped in June 2024 and 2.5 is the current release as of October 2026. The changes most likely to come up are NEP 50 scalar promotion, copy=False meaning never copy, removed aliases such as np.NaN and np.float_, the 64-bit default integer on Windows and the b shape rule in np.linalg.solve. Knowing what changed from 1.x is itself a strong signal.
How long does it take to prepare for NumPy interview questions?
If you already write Python daily, two to three weeks of focused practice is usually enough. Spend the first week on shapes, broadcasting and indexing until you can predict every result shape. Spend the second writing standard algorithms (softmax, attention, pairwise distances, k-means) without loops and checking them against slow reference versions. Use the remaining time on numerical stability, dtypes and memory questions, which separate strong candidates.
What level of NumPy do I need for an ML engineer role?
You should be able to write a forward pass, a loss and a gradient for a small network in NumPy, with batched shapes and no Python loops over examples. You should also explain views versus copies, broadcasting rules, einsum notation and why softmax subtracts a max. You do not need the C API or memory-mapped files, but knowing how strides and dtypes affect speed and memory is expected at senior level.
Should I use NumPy or PyTorch in a coding interview?
Use whatever the interviewer asks for, and NumPy when they leave it open for a math-heavy prompt, since it has no autograd or device handling to explain. The concepts transfer almost one to one: broadcasting, indexing and einsum behave the same in PyTorch, though names differ (keepdim versus keepdims, dim versus axis). Mention the differences you know, such as torch.std dividing by n - 1 by default while np.std divides by n.
What are the most common NumPy mistakes interviewers look for?
The usual ones are shape bugs from broadcasting (n, 1) against (n,), writing through a chained fancy index that silently does nothing, counts[ids] += 1 with repeated indices, integer overflow in uint8 or int32 arrays, comparing floats with ==, computing softmax without subtracting the max, and np.linalg.inv where solve belongs. Saying how you would detect each one, with an assert, np.shares_memory or a small test, scores as well as the fix.
Written by
Dan Lee
Data & AI Lead
Dan is a seasoned data scientist and ML coach with 10+ years of experience at Google, PayPal, and startups. He has helped candidates land top-paying roles and offers personalized guidance to accelerate your data career.