Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Practice and Review

Interview Questions

Twelve SciPy and numerical computing interview questions on conditioning, stiffness, p-values, local minima and sparse formats, with short runnable answers.

Interviewers for data, scientific and quantitative roles rarely ask you to recite function signatures. They ask why something behaves the way it does: why you should not invert a matrix, what a p-value really is, why an ODE solver takes a million steps. A good answer is short, correct and backed by a small experiment.

This lesson collects twelve such questions. Each has a concise answer you could say out loud, and the harder ones come with a short program that proves the point. Try to answer in your own words before reading. Where a question touches a topic in depth, the matching lesson is linked.

flowchart TD
    A["Interview question"] --> B["State the concept<br/>in one sentence"]
    B --> C["Give a concrete example<br/>or a number"]
    C --> D["Mention the pitfall<br/>or trade-off"]
    D --> E["Offer to demonstrate<br/>with a few lines of code"]

A reliable answer pattern: concept, example, pitfall, demonstration.

Linear algebra and numerical accuracy

1. Why should you call solve instead of inverting a matrix?

solve(A, b) factorizes A once (LU) and substitutes, which costs about a third of the work of forming an inverse and gives smaller rounding errors. inv(A) @ b computes extra numbers you do not need, then multiplies, and the multiplication adds error. For sparse matrices it is much worse: the inverse of a sparse matrix is usually dense, so you destroy the storage advantage. If you need to solve with many right-hand sides, factor once with lu_factor or splu and reuse the factors.

2. What is a condition number, and what does it tell you?

It measures how much the output of a problem can change, relatively, when the input changes slightly. For a linear system Ax = b the worst-case relative change in x is about cond(A) times the relative change in b (or in A). A condition number of 1e10 means you can lose about ten of your sixteen digits no matter how good the algorithm is. It is a property of the problem, not of the code.

Python Answer 2: sensitivity grows with the condition number

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The nearly parallel rows make the matrix almost singular. A nudge of 1e-8 in the data moves the answer by 1e-6, 1e-2 and 100 as the condition number rises. See Matrices, Solving Systems and Inverses for more.

3. When is np.isclose(a, b) true, and why do you need both rtol and atol?

It is true when |a - b| <= atol + rtol * |b|. The relative part handles numbers of any scale, but it cannot work when b is zero (the allowed difference would be zero), so the absolute part provides a floor. The defaults (rtol=1e-5, atol=1e-8) suit numbers of order one; choose your own when your quantities are tiny or huge. np.isclose(1e9 + 1, 1e9) is true and np.isclose(1e-9, 0) is true as well; the second would fail without atol.

Differential equations and optimization

4. What is a stiff ODE?

A stiff problem has components that change on very different time scales, for example a fast transient that dies out and a slow solution that you actually care about. An explicit method such as RK45 must keep its step small enough for the fast, long-gone component, so it takes enormous numbers of steps even when the solution is smooth. Implicit methods (Radau, BDF, LSODA) remain stable with much larger steps and cost more per step. Diagnose stiffness by seeing a huge nfev for a smooth solution.

Python Answer 4: counting function evaluations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All four agree on the answer, but RK45 needs roughly 35 to 45 times more evaluations than the implicit methods. See Stiff Systems and Higher-Order ODEs.

5. Why can minimize return a different answer from different starting points?

Local optimizers follow the slope downhill and stop at the first point where the gradient vanishes. A function with several valleys has several local minima, and the start decides which one you reach. Remedies: try many starting points (multi-start), use a global method (differential_evolution, dual_annealing, basinhopping), or reformulate the problem to be convex. Always check res.success as well.

Python Answer 5: three starts, three answers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Starting at 0 finds the deepest valley, while the starts at -3 and 2 settle into shallower ones. The global search finds the best.

Statistics

6. What is a p-value, and what is it not?

A p-value is the probability, assuming the null hypothesis is true, of seeing data at least as extreme as what you observed. It is not the probability that the null is true, not the probability that your result is a fluke, and not a measure of effect size. A tiny p-value can accompany a trivial effect if the sample is huge. Report an effect size and a confidence interval alongside it.

7. If the null hypothesis is true, what distribution do p-values follow?

For a continuous test statistic they are uniform between 0 and 1. That is why a 5 percent threshold makes a false alarm in 5 percent of tests. A histogram of p-values from many tests with no real effect is flat; a bump near zero signals real effects.

8. Why is running 20 tests at the 5 percent level a problem?

Every test is another chance for a false alarm. The probability of at least one false positive among 20 independent null tests is 1 - 0.95**20, about 64 percent. Corrections such as Bonferroni (divide the threshold by the number of tests) or the Benjamini-Hochberg procedure (controls the false discovery rate) restore control.

Python Answers 7 and 8: uniform p-values and multiple testing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For the full treatment see Hypothesis Testing.

9. Why does np.std differ from statistics.stdev or ddof=1?

By default NumPy divides by N (the population formula, ddof=0). For a sample drawn from a larger population, dividing by N - 1 (ddof=1) removes the bias in the variance estimate, because the sample mean was fitted from the same data. The difference matters for small samples and disappears for large ones. scipy.stats functions such as describe use the sample versions.

10. When would you prefer Spearman to Pearson correlation?

Pearson measures linear association and is sensitive to outliers. Spearman works on ranks, so it detects any monotonic relationship, and it is robust to outliers and to monotone transformations. For y = exp(x / 3) over x = 1..20, Spearman is exactly 1 because y always rises with x, while Pearson is about 0.8 because the relationship is curved. Try stats.pearsonr and stats.spearmanr on that data to confirm.

Sparse matrices and signals

11. How do the sparse formats differ, and which would you choose?

  • COO (coordinate): lists of row, column and value. Easy to build, and duplicate entries are summed when converted. It supports no arithmetic or slicing directly.
  • CSR (compressed sparse row): fast row slicing and matrix-vector products; the default workhorse for computation.
  • CSC (compressed sparse column): fast column slicing; the format spsolve prefers.
  • LIL (list of lists) and DOK (dictionary of keys): cheap to modify element by element, slow for arithmetic.

The standard workflow is to build in COO (or LIL when editing), convert once to CSR or CSC, and compute there.

Python Answer 11: duplicates in COO, building in LIL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

See Sparse Matrices for the details.

12. What is spectral leakage?

The FFT assumes the signal repeats forever. If the window contains a whole number of periods, the end joins the start smoothly and the spectrum shows a single sharp line. If not, the artificial jump at the seam smears energy across many bins. Tapering the signal with a window function (Hann, Hamming) reduces the effect at the price of a wider main peak.

Python Answer 12: whole periods versus partial periods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At 8 Hz there are exactly 8 periods and one bin lights up. At 8.5 Hz the peak drops to about 0.65 and 33 bins carry visible energy. More in The Fast Fourier Transform.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A matrix has condition number 1e12. Roughly how many digits of a double-precision solution can you trust?

    Double precision has about 16 digits. Losing roughly log10(cond) = 12 of them leaves about 4.
  2. 2. Which sentence about a stiff ODE is correct?

    Stability, not accuracy, limits the step size of an explicit method on a stiff problem. Implicit methods remove that limit.
  3. 3. If a null hypothesis is true and you run one test at the 5 percent level, what is the probability of a false positive?

    By definition of the significance level, p-values are uniform under the null, so p < 0.05 happens 5 percent of the time.
  4. 4. You build a sparse matrix from COO input in which position (0, 1) appears twice. What happens on conversion to CSR?

    COO allows duplicates and sums them when converting. This is handy for assembling matrices from many small contributions.
  5. 5. Why does minimize give different answers from different starting points on a function with several valleys?

    Gradient-based local methods only know the slope near the current point. Multi-start or global methods explore more of the landscape.

Summary

  • Prefer solve over inv; the condition number, a property of the problem, caps the accuracy of any algorithm.
  • Stiff ODEs need implicit solvers (Radau, BDF, LSODA); a huge function-evaluation count for a smooth solution is the warning sign.
  • Local optimizers find local minima; use multi-start or global methods and always check success.
  • A p-value is computed assuming the null; under the null it is uniform, and many tests require a correction.
  • Use ddof=1 for sample variance, and Spearman instead of Pearson for monotone but nonlinear relationships.
  • Know the sparse formats (COO to build, CSR/CSC to compute, LIL to edit) and remember that sparse inverses are dense.
  • Windowing tames spectral leakage in the FFT.

Finish the course with a compact reference: the SciPy Cheat Sheet.