NumPy Interview Questions

Dan Lee's profile image
Dan LeeData & AI Lead
Last update
NumPy Interview Questions

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.

Machine Learning EngineerData ScientistResearch EngineerAI EngineerQuantitative AnalystQuantitative Developer

Arrays, dtypes and memory layout

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
2
3a = np.arange(6)
4b = a[::2]             # basic slice: a view
5c = a[[0, 2, 4]]       # integer-array index: a copy
6b[0] = 99
7c[1] = -1
8print(a)                                               # [99  1  2  3  4  5]
9print(np.shares_memory(a, b), np.shares_memory(a, c))  # True False
10print(b.base is a, c.base is None)                     # True True
Learn this in the LLM Architecture course

Broadcasting rules and traps

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
2
3X = 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:
8    print(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)
Learn this in the LLM Architecture course

Indexing: basic, fancy and boolean

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
2
3a = np.array([3, -1, 4, -2, 5])
4a[a < 0][0] = 0                   # writes into a temporary copy
5print(a)                          # [ 3 -1  4 -2  5]
6a[a < 0] = 0                      # one indexing call on a itself
7print(a)                          # [3 0 4 0 5]
8b = np.array([3, -1, 4, -2, 5])
9b[np.flatnonzero(b < 0)[0]] = 0   # first negative only
10print(b)                          # [ 3  0  4 -2  5]
Learn this in the LLM Architecture course

Vectorization, ufuncs and performance

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
2
3x = np.random.default_rng(0).normal(size=1_000_000)
4def tier(v):
5    return 2.0 if v > 1 else (1.0 if v > 0 else 0.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()))                   # True
9t1 = timeit.timeit(lambda: slow(x), number=3) / 3
10t2 = timeit.timeit(fast, number=3) / 3
11print(f"vectorize {t1*1e3:.0f} ms, select {t2*1e3:.1f} ms")
Learn this in the LLM Architecture course

Numerics, linear algebra and random numbers

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

Python
1import numpy as np
2
3print(0.1 + 0.2 == 0.3, np.isclose(0.1 + 0.2, 0.3))   # False True
4print(np.isclose(1e-9, 2e-9))                         # True: atol dominates
5print(np.isclose(1e-9, 2e-9, atol=0))                 # False
6print(np.isclose(1.0, 1.00001, atol=0), np.isclose(1.00001, 1.0, atol=0))  # True False
7print(np.allclose([1, np.nan], [1, np.nan], equal_nan=True))           # True
Learn this in the LLM Architecture course

How to Prepare for NumPy Interviews

Annotate the shape of every line

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.

Dan Lee's profile image

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.

Connect on LinkedIn