Interview Questions: NumPy for Data Analysis
NumPy comes up in nearly every data-adjacent Python interview, whether the role is data analysis, ML, or backend engineering with numeric workloads. This set covers arrays vs. lists, array creation, indexing/slicing, boolean masking, vectorized math and broadcasting, aggregate functions and the axis= parameter, and reshaping/stacking.
1 Why is a NumPy array typically faster than an equivalent Python list for numeric operations?
A Python list can hold mixed types, so every arithmetic operation on it re-checks each element's type at runtime and pays normal Python function-call overhead. A NumPy array locks in one fixed dtype for every element up front, so vectorized operations (arr.sum(), arr * 2) skip that repeated type-checking and run as a tight, compiled loop instead of an interpreted Python loop — often 10-100x faster on anything beyond a handful of values.
Vague answers like "arrays are just faster" with no mechanism given — interviewers want to hear "fixed dtype" and "vectorized/compiled operations," not just a speed claim.
Follow-up"Does NumPy make EVERY operation faster, even a single addition on two numbers?" (No — for a tiny amount of data, the overhead of creating a NumPy array can outweigh the benefit; the speed advantage shows up at scale, not on trivial inputs.)
2 What's the difference between np.arange() and np.linspace()?
np.arange(start, stop, step) is defined by a step size — you specify the gap between consecutive values, and the resulting count is whatever that produces (similar to Python's range()). np.linspace(start, stop, count) is defined by count — you specify exactly how many evenly spaced values you want, including both endpoints, and NumPy computes the step size automatically.
Claiming the two are interchangeable, or not knowing that linspace() includes the stop value by default while arange() excludes it.
"If you need exactly 100 evenly spaced points between 0 and 1, which would you use?" (np.linspace(0, 1, 100) — you know the desired COUNT, not a convenient step size.)
3 What do .shape, .dtype, and .ndim each tell you about an array?
.shape is a tuple giving the size along each dimension (e.g. (3, 4) for 3 rows and 4 columns). .dtype is the single data type every element shares (int64, float64, etc.). .ndim is the number of dimensions (1 for a flat array, 2 for a grid, etc.) — and is always equal to the length of .shape.
Confusing .shape with the total element count (it's per-dimension sizes, not one number) or not knowing .ndim is derivable from len(.shape).
"A bug is producing unexpected results from an array operation. Which of these three would you check first?" (Usually .shape — the vast majority of confusing NumPy bugs trace back to an array having a different shape than expected, especially after slicing, indexing, or an axis= aggregate.)
4 How does 2D indexing differ syntactically from Python's nested-list indexing?
A nested Python list uses chained indexing, grid[1][2] — each bracket indexes one level deeper. A NumPy 2D array supports one comma-separated index instead, grid[1, 2], reading as "row 1, column 2" in a single lookup. A bare : in either position means "every element on this dimension" — grid[:, 1] is the whole second column, grid[1, :] (or just grid[1]) is the whole second row.
Assuming grid[1][2] doesn't work on a NumPy array at all (it does, but it's slower and less idiomatic than the comma form, since it creates an intermediate array first).
"What does grid[:, 1] return if grid has shape (3, 4)?" (A 1D array of shape (3,) — the entire second column, one value from each of the 3 rows.)
5 What is boolean masking, and why is it considered NumPy's single most important idiom?
A comparison like arr > 5 produces a new array of True/False values, the same shape as arr, called a boolean mask. Indexing with that mask (arr[arr > 5], or arr[mask]) returns only the elements where it's True. It's the most important idiom because it replaces an entire loop-plus-if-plus-manual-list-building pattern with one expression, and it's exactly how real-world filtering ("every sensor reading above a threshold," "every failing grade") gets expressed in NumPy-based code.
Describing masking as "just like a Python list comprehension with an if" without recognizing it runs vectorized, with no explicit loop at all, and returns a NumPy array, not a Python list.
Follow-up"How would you combine two conditions in a single mask, like values between 5 and 10?" (Use &/| instead of and/or, with each condition parenthesized: arr[(arr > 5) & (arr < 10)].)
6 What is broadcasting, and what's the general rule for when it applies?
Broadcasting is NumPy's rule for combining arrays of different but compatible shapes in an arithmetic operation, by conceptually "stretching" the smaller array across the larger one's shape without actually copying data. Comparing shapes from the rightmost dimension inward, two dimensions are compatible if they're equal or if one of them is 1 (which gets stretched). A single number broadcasts across an entire array; a 1D array broadcasts across each row of a 2D grid if its length matches the number of columns.
Red flagsDescribing broadcasting as "NumPy just figures it out somehow" without being able to state the actual rule (equal, or one of them is 1).
Follow-up"Would a shape-(3,) array and a shape-(4,3) array broadcast together?" (Yes — the (3,) array's missing leading dimension is treated as size 1, so it's compared as (1,3) against (4,3): the rightmost dimensions match (3 and 3), and the size-1 dimension stretches across all 4 rows.)
7 What happens when two arrays' shapes genuinely can't broadcast together?
NumPy raises a ValueError that names both mismatched shapes (e.g. "operands could not be broadcast together with shapes (3,) (2,)"), rather than silently truncating, padding, or guessing at an answer. This fail-loudly behavior is deliberate — a silent wrong answer on numeric data is far more dangerous than a clear, immediate error.
Assuming NumPy will pad the shorter array with zeros or truncate the longer one to "make it work" — it does neither; a genuine mismatch is always an error.
Follow-up"How would you fix a shape (3,) + (2,) mismatch if the intent was actually valid?" (Depends on the real intent — reshape one array so its shape genuinely aligns with the other, e.g. to (3,1) or (1,2), rather than trying to force incompatible data together.)
8 Walk through what axis=0 and axis=1 each do on a 2D array's aggregate functions.
axis=0 collapses DOWN the rows, producing one result per COLUMN — if rows are students and columns are subjects, grades.mean(axis=0) gives one average per subject. axis=1 collapses ACROSS the columns, producing one result per ROW — grades.mean(axis=1) gives one average per student. With no axis= given at all, the aggregate collapses the entire array to a single number.
Getting axis=0 and axis=1 backwards — extremely common, and worth catching in an interview by explicitly checking result.shape rather than guessing from memory.
"For a (3, 5) grid, what shape does .sum(axis=0) return? What about .sum(axis=1)?" (axis=0 -> shape (5,), one value per column; axis=1 -> shape (3,), one value per row — the axis you specify is the dimension that disappears from the result.)
9 What does .std() measure, and why would a report include it alongside mean/min/max?
Standard deviation measures how spread out values are around the mean — a small .std() means most values sit close to the average, a large one means they're spread widely. Alongside mean/min/max, it gives a quick sense of consistency: two datasets can share the same mean but have very different spreads, and .std() is what distinguishes "everyone scored close to 85" from "scores ranged wildly but averaged to 85."
Confusing standard deviation with range (max - min) — they're related but not the same measure, and a candidate should know std specifically reflects spread AROUND the mean, not just the total spread.
Follow-up"If two classes both average 80 on a test but one has std=2 and the other std=15, what does that tell you?" (The std=2 class scored very consistently near 80; the std=15 class had much more variation — some students far above or below 80 — even though both averages are identical.)
10 What does .reshape(rows, cols) actually do to the underlying data?
.reshape() reinterprets the same sequence of values with a new shape — it does not reorder, recompute, or copy the underlying data, it just changes how that same sequence is grouped into rows and columns. The one hard requirement is that the total element count must stay the same: reshaping a 6-element array works into (2,3) or (3,2) or (6,1), but not into (2,4), since 8 ≠ 6.
Believing reshape can "add" or "remove" data to make a shape fit — it can't; a mismatched element count always raises an error.
Follow-up"You have 168 hourly sensor readings as a flat array and want a daily average. What reshape would you use?" (.reshape(7, 24) — 7 days of 24 hours each — then .mean(axis=1) to average across each day's 24 hourly readings.)
11 What's the difference between np.concatenate(), np.vstack(), and np.hstack()?
All three combine separate arrays into one, but differently: np.concatenate() joins arrays end-to-end along an existing dimension (for two 1D arrays, this produces one longer 1D array). np.vstack() stacks arrays as new rows of a 2D result. np.hstack() joins arrays side by side — for two 1D arrays this looks identical to concatenate, but the difference becomes visible once the inputs are already 2D, where hstack joins along columns rather than adding new rows.
Treating all three as fully interchangeable in every case — they agree on flat 1D inputs but diverge on 2D inputs, and a candidate should know that distinction exists even if the exact 2D behavior isn't memorized.
Follow-up"If you have two 1D arrays of readings from two different sensors and want them as two separate rows of one grid, which function?" (np.vstack() — it explicitly creates a new row per input array, which concatenate() alone would not do for 1D inputs.)
12 Why does NumPy underlie so many other Python data-science libraries?
NumPy's ndarray, its shape conventions, and its broadcasting rules became the shared, de facto standard representation for numeric data in Python. Rather than each library (pandas, scikit-learn, matplotlib, and parts of TensorFlow/PyTorch) inventing its own array type and conversion rules, they build directly on NumPy arrays or expose NumPy-compatible interfaces — meaning skills learned here (shapes, broadcasting, axis=) transfer directly instead of needing to be relearned per library.
Describing NumPy as "just one option among many" for numeric Python work, missing that its conventions are effectively the industry-standard foundation other tools are built on top of.
Follow-up"If you already understand NumPy's axis= parameter well, does that help with pandas?" (Yes — pandas' axis= parameter on its own aggregate methods follows the same axis=0/axis=1 convention NumPy established, so the mental model transfers directly.)
13 When would you choose a plain Python list over a NumPy array?
When the data is genuinely mixed-type (strings, dicts, and numbers together), when the collection needs to grow/shrink frequently and unpredictably (NumPy arrays are fixed-size; growing one means creating a new array), or when the dataset is small enough that vectorization's performance benefit doesn't matter and a list's simplicity and flexibility are more valuable. NumPy is specifically the right tool for large, uniform, numeric data that needs fast bulk operations — not a universal list replacement.
Red flagsClaiming NumPy arrays are strictly "better" in every situation, missing that lists remain the right default for general-purpose, mixed-type, or frequently-resized Python data.
Follow-up"Would you use a NumPy array to store a list of student names alongside their grades?" (Not directly as one array — names are strings and grades are numbers; you'd typically keep them as a list of dicts/tuples, or two parallel arrays/lists, one per data type, rather than forcing mixed data into one array.)
14 What's the practical difference between arr.sum() with no axis and arr.sum(axis=0) on a 1D array specifically?
On a 1D array, they're identical — there's only one axis (axis 0), so specifying it explicitly changes nothing. The distinction only becomes meaningful on 2D-or-higher arrays, where axis=0 and axis=1 collapse different dimensions and no-axis-at-all collapses everything to one number. This is a good check for whether someone actually understands axis= as "which dimension to collapse" rather than having memorized it as some other kind of flag.
Red flagsAssuming axis= behaves differently even on 1D data, or being unable to explain why it "does nothing extra" there.
Follow-up"Does the same equivalence hold on a 2D array?" (No — on a 2D array, arr.sum() (no axis) collapses to one grand total, while arr.sum(axis=0) collapses only the rows, leaving one result per column; they genuinely differ once there's more than one dimension.)
Rapid-Fire Review
Quick recall check — fill in each blank and hit Check.