🧠 Neural Networks & Backpropagation — AI / ML Interview Guide

Neural Foundations · interactive visualization + interview prep

Open the interactive Neural Networks & Backpropagation visualization on PrepGrind → Step through a live animation, tune the parameters, and read the full theory, math, reference code, and interview Q&A below — free, in your browser.

What it is

A neural net is just a stack of "multiply, add a bias, then bend" operations. Each layer takes the previous layer's outputs, mixes them with a matrix of weights, adds a bias, and squashes the result through a nonlinear activation. Stacking these lets the network compose simple features into complex ones. "Learning" means searching for the weights that make the output match the labels — and we do that by measuring how wrong we are (the loss) and rolling every weight a small step downhill on that loss surface. Backpropagation is the bookkeeping trick that tells us which direction is downhill for every weight at once: it is the chain rule applied layer by layer, reusing each layer's gradient to cheaply compute the layer before it.

Mental model

A neural net is a differentiable function-fitter: a machine covered in knobs (weights) that turns inputs into outputs. For any knob you can ask "which way do I turn it to make the answer less wrong?" — that answer is the gradient. Training is just: ask that question for every knob at once (backprop), turn them all a hair in the better direction (gradient step), repeat. Everything else is detail about how to ask the question cheaply and how big a hair to turn.

Theory

A feed-forward network is a composition of simple functions: each layer applies an affine map (a = Wx + b) followed by a non-linear activation σ. Stacking L of these gives f(x) = σ(W_L · … σ(W₁x + b₁) … + b_L). The Universal Approximation Theorem says even one sufficiently wide hidden layer can approximate any continuous function — but depth lets the network build that function compositionally (simple features → complex features), which is far more parameter-efficient than width alone.

The non-linearity is load-bearing. Remove it and the whole stack collapses: a composition of linear maps is itself a single linear map, so a 50-layer linear net has exactly the power of one linear layer and can never solve a non-linearly-separable problem like XOR. σ is what lets stacked layers carve curved decision regions.

Learning is framed as optimization. We define a loss L(θ) measuring how wrong the outputs are over the data (MSE for regression, cross-entropy for classification), then search the weight space θ for a minimum. Because the network is differentiable end-to-end, we can compute ∇L and use gradient descent — see the Gradient Descent concept for how the step itself works.

Backpropagation is how we get ∇L efficiently. It is reverse-mode automatic differentiation: the chain rule applied from the loss backward, reusing each layer's computed error δ to produce the layer before it. One forward pass caches activations; one backward pass yields the gradient of EVERY parameter. The cost is the same order as the forward pass — without this reuse, training billion-parameter models would be impossible.

Two structural facts drive most practical issues. First, gradients are products of many per-layer terms, so they can vanish (→0) or explode across depth — motivating ReLU activations, careful initialization, residual connections, and normalization. Second, minimizing training loss is not the goal: generalization is, so we watch a held-out metric and regularize (weight decay, dropout, early stopping) to avoid memorizing the training set.

Concrete example

Learning XOR is the classic "aha". XOR outputs 1 when exactly one input is on: (0,0)->0, (0,1)->1, (1,0)->1, (1,1)->0. No straight line can separate the 1s from the 0s, so a plain linear model (logistic regression / a single neuron) can NEVER solve it. Add one hidden layer with a nonlinear activation and the network learns to carve the plane into the right regions: the hidden units learn intermediate features (roughly "OR" and "AND"), and the output combines them into XOR. Watch the decision-boundary canvas: it starts as noise, then bends into the checkerboard shape as the loss drops. The same machinery scales up to, say, a spam classifier: inputs are word/feature counts, hidden layers learn combinations ("contains a sketchy link AND urgency words"), and the output neuron emits P(spam).

Key equations

Step by step

  1. Forward pass: feed the inputs x in as a⁽⁰⁾. For each layer compute z = W·a + b, then a = σ(z), passing activations forward until the output layer produces a prediction.
  2. Measure loss: compare the prediction a⁽ᴸ⁾ to the true label y with the loss function (MSE here, cross-entropy for classification). This single number is what we drive toward zero.
  3. Output error: compute δ⁽ᴸ⁾ = ∇ₐL ⊙ σ′(z⁽ᴸ⁾) — how much a tiny change in each output neuron's pre-activation changes the loss.
  4. Backpropagate: push δ backward with δ⁽ˡ⁾ = (W⁽ˡ⁺¹⁾ᵀ·δ⁽ˡ⁺¹⁾) ⊙ σ′(z⁽ˡ⁾). Each layer's error is the next layer's error routed back through the same weights and scaled by the local activation slope.
  5. Gradients: for every edge, ∂L/∂w = δ(downstream) · a(upstream); for every neuron, ∂L/∂b = δ. (Hover an edge in the canvas to see its live weight and grad.)
  6. Update: nudge each weight and bias against its gradient, w ← w − η·∂L/∂w. A small learning rate means small, stable steps; too large overshoots.
  7. Repeat: one pass over the data is an epoch. The loss chart should trend downward (with wiggle from SGD) as the decision boundary sharpens.

Interview questions & answers

Why do we need nonlinear activation functions?

Without them, every layer is a linear map, and composing linear maps gives just another linear map — so a 50-layer net would have exactly the expressive power of one linear layer and could never solve XOR or any non-linearly-separable problem. The nonlinearity (ReLU, sigmoid, tanh) is what lets stacked layers represent curved, complex decision boundaries.

What is backpropagation, really, and why do we need it?

It is the chain rule applied across the network to compute ∂L/∂w for every weight efficiently. Naively, estimating each gradient by perturbing one weight at a time would cost a full forward pass per weight — millions of passes. Backprop reuses intermediate results so one forward + one backward pass yields ALL gradients, making training tractable.

What causes vanishing / exploding gradients?

In δ⁽ˡ⁾ = (W⁽ˡ⁺¹⁾ᵀδ⁽ˡ⁺¹⁾) ⊙ σ′(z⁽ˡ⁾), the error is repeatedly multiplied by weights and activation slopes as it flows back. Sigmoid/tanh saturate, so σ′ ≤ 0.25 — chain many layers and the gradient shrinks toward 0 (vanishing); if weights are large the product blows up (exploding). Fixes: ReLU-family activations, careful init (Xavier/He), residual connections, batch/layer norm, and gradient clipping.

Batch vs mini-batch vs stochastic gradient descent — trade-offs?

Full-batch GD uses the whole dataset per step: smooth, accurate gradients but slow and memory-heavy. SGD uses one example: noisy but fast updates, and the noise can help escape sharp minima. Mini-batch (e.g. 32–512) is the practical middle ground — stable enough to converge, large enough to use vectorized/GPU math, small enough for frequent updates. Almost all real training uses mini-batches.

How does the learning rate affect training?

Too small: training crawls and may stall in a plateau. Too large: steps overshoot the minimum, the loss oscillates or diverges to NaN. In practice people use a schedule (warmup then decay) or adaptive optimizers (Adam) that effectively tune a per-parameter rate. Try sweeping the lr slider here and watch the loss chart.

Why initialize weights randomly instead of all zeros?

With identical weights, every neuron in a layer computes the same output and receives the same gradient, so they update identically and stay identical forever — the layer has the expressive power of a single neuron (the "symmetry breaking" problem). Small random init (scaled by fan-in, e.g. He/Xavier) breaks symmetry and keeps activation/gradient variance stable across layers.

How do you tell overfitting from underfitting, and what do you do?

Underfitting: both train and validation loss are high — the model is too weak, so add capacity (more units/layers), train longer, or reduce regularization. Overfitting: train loss keeps dropping while validation loss rises — the model memorizes noise, so add regularization (L2/weight decay, dropout), get more data, augment, or early-stop at the validation minimum.

Common pitfalls

Where it shows up

More AI / ML interview concepts

PrepGrind runs entirely in your browser, free, no installation required. Loading the interactive playground…