skip to content

Calculus & Optimization Basics

You will learn gradients, the chain rule, convexity, and why gradient descent finds minima — the calculus that makes 'training a model' a meaningful sentence. Interviewers probe it to see whether you can explain what a loss surface is and what happens when optimization goes wrong.

on this pageshow

explore

questions

page 1 of 2

What is the formal definition of a convex set?

level: juniorimportance: must knowfreq 66%

answer

  1. think about straight lines
  2. pick any two points of the set
  3. the whole segment must stay inside
  4. t*x + (1-t)*y for t in [0,1]

basics

~20 s

A set is convex if the straight segment joining any two of its points lies entirely inside it: for every x and y in the set C and every t in [0, 1], the point t*x + (1-t)*y is also in C.

solid answer

~40 s

A set `C` is convex when, for all `x, y` in `C` and all `t` in `[0, 1]`, the convex combination `t*x + (1-t)*y` belongs to `C`. Geometrically: pick any two points of the set, draw the segment between them, and the segment never leaves the set. Half-planes such as `{(x, y) : x + y <= 3}`, balls, boxes and the L1 ball `{x : sum |x_i| <= 1}` are convex; a crescent, an annulus like `1 <= x^2 + y^2 <= 4`, and the union of two disjoint disks are not, because you can pick two points whose connecting segment leaves the region. Convexity survives intersection, translation, scaling and affine maps, which is why feasible regions built from linear constraints are automatically convex.

go deeper

for a junior

Be ready to state the segment condition precisely and to classify simple shapes on sight: disk yes, half-plane yes, annulus no, union of two disks no.

for a middle

Explain why the condition is stated with a parameter t in [0,1], and show that intersections preserve convexity while unions do not, with the one-line segment argument.

for a senior

Demonstrate that you build convexity rather than check it: recognise a feasible region as an intersection of half-spaces or norm balls, and know which operations you may apply without re-proving anything.

for a principal

Own the modelling call — whether to express a constraint set so that it is convex at all, since the choice between a convex relaxation and an exact non-convex description determines what solver guarantees you can promise.

## The definition A set `C` in `R^n` is **convex** if for every pair of points `x, y` in `C` and every scalar `t` in `[0, 1]`, ``` t*x + (1 - t)*y is in C ``` The expression `t*x + (1 - t)*y` is called a **convex combination** of `x` and `y`. As `t` sweeps from `0` to `1`, the point traces the straight segment from `y` to `x`. So the definition says exactly: *the segment between any two points of the set stays inside the set*. Nothing about smoothness, symmetry or roundness is required. The definition extends to more than two points: `C` is convex if and only if every convex combination `t_1*x_1 + ... + t_k*x_k` (with `t_i >= 0` and `sum t_i = 1`) of points of `C` is again in `C`. The two-point version already implies the general one by induction. ## Examples that are convex - A **half-space**: `{x : a . x <= b}` for a fixed vector `a` and scalar `b`. If two points each satisfy the inequality, so does every point between them, because `a . x` is linear in `x`. - Any **ball** in any norm, including the Euclidean ball `{x : ||x||_2 <= 1}` and the **L1 ball** `{x : sum |x_i| <= 1}` (the diamond). Norm balls are convex because norms satisfy the triangle inequality and positive homogeneity. - A **box** `{x : l_i <= x_i <= u_i}`, which is an intersection of half-spaces. - The whole space, a single point, and the empty set (vacuously). ## Examples that are not convex - An **annulus** such as `1 <= x^2 + y^2 <= 4`. Take the points `(-1.5, 0)` and `(1.5, 0)`: both are in the ring, but their midpoint `(0, 0)` is in the hole, so the segment escapes. - A **crescent** or any shape with a dent: the segment across the dent leaves the region. - The **union of two disjoint disks**: a segment from one disk to the other passes through empty space. - A **curve** such as the circle `x^2 + y^2 = 1` on its own (as opposed to the filled disk): the chord between two points of the circle is inside the disk, not on the circle. ## Operations that preserve convexity This is the practical part, because most convex sets you meet are built rather than checked from scratch: - **Intersection** of any number of convex sets is convex. If a segment lies in each set, it lies in all of them at once. This is why a feasible region defined by many linear inequalities is convex. - **Translation** `C + v`, **scaling** `a*C`, and more generally **affine images** `{A*x + b : x in C}` are convex. - **Minkowski sum** `{x + y : x in C, y in D}` of two convex sets is convex. - **Union** is the notable failure: the union of two convex sets is generally not convex. ## The bridge to convex functions Convex sets and convex functions are two views of the same idea. A function `f` is convex exactly when its **epigraph** — the set of points lying on or above its graph, `{(x, t) : t >= f(x)}` — is a convex set in one higher dimension. In the other direction, every **sublevel set** `{x : f(x) <= c}` of a convex function is a convex set. The converse of that last statement is false: `f(x) = sqrt(|x|)` has sublevel sets `[-c^2, c^2]`, which are intervals and therefore convex, yet the function itself is not convex. Functions whose sublevel sets are all convex are called quasiconvex, a strictly weaker property. ## Why interviewers ask Convexity of the feasible region is half of what makes an optimisation problem tractable — the other half is convexity of the objective. Being able to say *why* a region qualifies (it is an intersection of half-spaces, it is a norm ball) rather than pointing at a picture is the difference between recognition and understanding.

  • Is the intersection of two convex sets always convex, and what about their union?
    The intersection always is: if a segment lies inside each set, it lies inside both, hence inside the intersection. The union generally is not — two disjoint disks are each convex, but a segment from one to the other passes through empty space. This asymmetry is why feasible regions built from many constraints (an intersection) stay convex.
  • How does the epigraph connect convex sets to convex functions?
    The epigraph of `f` is the set of points on or above its graph: `{(x, t) : t >= f(x)}`. A function is convex if and only if its epigraph is a convex set in one higher dimension. That equivalence lets you transfer every fact about convex sets to convex functions and back, and it is the standard way the two definitions are unified.
  • Are the sublevel sets of a convex function convex, and does the converse hold?
    Yes for the forward direction: `{x : f(x) <= c}` is convex whenever `f` is convex, since the chord bound keeps intermediate points below `c`. The converse fails. `f(x) = sqrt(|x|)` has sublevel sets `[-c^2, c^2]`, which are convex intervals, yet the function is not convex. Such functions are called quasiconvex.

Think of the set as a room. It is convex if, standing anywhere inside, you can see every other point of the room without a wall blocking the line of sight.

saying these in an interview costs you the question

  • Confuses a convex set with a convex function
  • Claims the union of convex sets is always convex
  • Says an annulus is convex because it is round
  • Requires the boundary to be smooth or curved outward
  • Checks only endpoints instead of the whole segment

context

open as a page

Why is a zero gradient necessary but not sufficient for a local minimum?

level: juniorimportance: must knowfreq 82%

basics

~20 s

At a smooth interior local minimum the gradient must vanish, so a zero gradient is necessary. But maxima, saddle points and flat inflection points also have a zero gradient, so vanishing slope on its own certifies nothing.

open as a page

Using the limit definition of the derivative, what is the derivative of f(x) = x^2?

level: juniorimportance: must knowfreq 72%

basics

~10 s

The derivative of x^2 is 2x. The difference quotient ((x+h)^2 - x^2)/h expands to (2xh + h^2)/h, which simplifies to 2x + h, and letting h shrink to 0 leaves 2x.

open as a page

In gradient descent, what does the update rule x = x - eta * gradient actually do at each step?

level: juniorimportance: must knowfreq 88%

basics

~20 s

Each iteration moves every parameter a short distance opposite its own partial derivative: new value = old value minus eta times the gradient. The learning rate eta scales how far you move; the loop repeats until the gradient is near zero.

open as a page

For f(x, y) = x^2 * y, what are the two partial derivatives and the gradient at (2, 3)?

level: juniorimportance: must knowfreq 82%

basics

~20 s

Differentiate one variable at a time, holding the other fixed: df/dx = 2xy and df/dy = x^2. At (2, 3) those evaluate to 12 and 4, so the gradient there is the vector (12, 4).

open as a page

For a map f from R^n to R^m, what shape is the Jacobian and what is entry (i, j)?

level: middleimportance: must knowfreq 68%

basics

~20 s

The Jacobian is m by n: one row per output component, one column per input variable. Entry (i, j) is the partial derivative of output component f_i with respect to input x_j, evaluated at a specific point.

open as a page

Using the chord test, why is f(x) = x^2 convex but f(x) = x^3 not?

level: middleimportance: must knowfreq 70%

basics

~20 s

A function is convex when every chord lies on or above its graph: f(t*x + (1-t)y) <= tf(x) + (1-t)*f(y). Squaring passes everywhere. Cubing fails between x = -2 and 0, where f(-1) = -1 sits above the chord value -4.

open as a page

Why is the origin a saddle point of f(x, y) = x^2 - y^2 rather than a minimum?

level: middleimportance: must knowfreq 68%

basics

~20 s

The gradient of x^2 - y^2 vanishes at the origin, but the surface curves up along the x-axis and down along the y-axis. A point that is a minimum one way and a maximum another is a saddle.

open as a page

Why is the absolute value function f(x) = |x| not differentiable at x = 0?

level: middleimportance: must knowfreq 66%

basics

~10 s

At 0 the difference quotient |h|/h equals +1 for positive h and -1 for negative h, so the two one-sided limits disagree and no single tangent slope exists. The function is still continuous there.

open as a page

For gradient descent on f(x) = x^2, which learning rates converge and which diverge?

level: middleimportance: must knowfreq 72%

basics

~20 s

On f(x) = x^2 the update multiplies x by (1 - 2 * eta) each step, so it converges exactly when eta is between 0 and 1. eta = 0.5 lands on the minimum in one step, eta between 0.5 and 1 oscillates while shrinking, eta = 1 cycles forever, and eta above 1 diverges.

open as a page

Why does the gradient of a differentiable function point in the direction of steepest ascent?

level: middleimportance: must knowfreq 74%

basics

~20 s

The rate of change in a unit direction equals the gradient's length times the cosine of the angle between them. Cosine peaks at 1 when the direction aligns with the gradient, so that bearing rises fastest.

open as a page

What is the Hessian matrix of a scalar function, and when is it symmetric?

level: middleimportance: must knowfreq 72%

basics

~20 s

The Hessian is the square matrix of all second partial derivatives of a scalar function, with entry (i, j) equal to d2f/dxi dxj. It is symmetric wherever those second partials are continuous, by Clairaut's theorem.

open as a page

How do Lagrange multipliers solve maximizing x*y subject to x + y = 10?

level: middleimportance: must knowfreq 60%

basics

~20 s

Form L = xy - lambda(x + y - 10) and set every partial derivative to zero. The solution is x = y = 5 with lambda = 5, so the largest achievable product is 25.

open as a page

In Newton's method for minimization, what does multiplying the gradient by the inverse Hessian achieve?

level: middleimportance: must knowfreq 60%

basics

~10 s

The inverse Hessian rescales the gradient by local curvature, giving long steps in flat directions and short ones where the surface curves sharply. The step lands on the minimizer of the local quadratic model.

open as a page

How do you derive the gradient of the loss L = ||Wx - y||^2 with respect to the matrix W?

level: seniorimportance: must knowfreq 55%

basics

~20 s

Set the residual r = Wx - y, so L = r transpose r. Entry W_ij affects only r_i, and does so through x_j, so the gradient is 2 (Wx - y) x transpose, a matrix with the same shape as W.

open as a page

What do the KKT conditions require at the optimum of an inequality-constrained problem?

level: seniorimportance: must knowfreq 48%

basics

~20 s

For minimizing f subject to g(x) <= 0 and h(x) = 0, the KKT conditions require stationarity of the Lagrangian, primal feasibility, nonnegative multipliers on the inequalities, and complementary slackness - every inequality's multiplier times its constraint value is zero.

open as a page

What is the second-order Taylor expansion of log(1 + x) about x = 0?

level: juniorimportance: should knowfreq 46%

basics

~10 s

log(1 + x) is approximately x - x^2/2 near x = 0. The dropped remainder starts at the cubic term x^3/3, so the error shrinks roughly eightfold each time x is halved.

open as a page

Why does Newton-Raphson converge quadratically when solving x^2 - 2 = 0 for sqrt(2)?

level: juniorimportance: should knowfreq 44%

basics

~20 s

Newton-Raphson replaces the function by its tangent line at the current guess, so each new error is roughly proportional to the square of the previous one. Near the root the number of correct digits roughly doubles per step.

open as a page

In the chain rule for f(g(h(x))), why is the total Jacobian the product J_f J_g J_h in that order?

level: middleimportance: should knowfreq 52%

basics

~20 s

Each factor must consume the output of the stage before it, so shapes conform only outermost-first: (m by q)(q by p)(p by n) yields the m by n total derivative. Matrix products are not commutative, so no other order works.

open as a page

What is the second-order test for convexity of a twice-differentiable function?

level: middleimportance: should knowfreq 55%

basics

~20 s

On an open convex domain, a twice-differentiable function is convex exactly when its Hessian is positive semidefinite at every point of that domain. In one variable this reduces to f''(x) >= 0 everywhere. A positive definite Hessian everywhere implies strict convexity.

open as a page

How does the sign of f''(c) classify a stationary point c of a one-variable function?

level: middleimportance: should knowfreq 60%

basics

~10 s

At a stationary point c: positive f''(c) means a strict local minimum, negative means a strict local maximum, and f''(c) = 0 is inconclusive, since minima, maxima and inflections all produce it.

open as a page

Does continuity of a function at a point imply that it is differentiable there?

level: middleimportance: should knowfreq 48%

basics

~10 s

No. The implication runs one way only: differentiability forces continuity, never the reverse. The function x times sin(1/x), with value 0 at the origin, is continuous at 0 yet its difference quotient oscillates forever.

open as a page

Why does the logistic function s(x) = 1/(1 + e^-x) have derivative s(x)(1 - s(x))?

level: middleimportance: should knowfreq 55%

basics

~20 s

The quotient rule turns 1/(1 + e^-x) into e^-x/(1 + e^-x)^2. That expression splits into 1/(1 + e^-x) times e^-x/(1 + e^-x), and the second factor is exactly 1 minus the first, giving s times (1 - s).

open as a page

Why does gradient descent zig-zag on an elliptical bowl with condition number 100?

level: middleimportance: should knowfreq 58%

basics

~20 s

The steepest direction limits the step size while the flattest direction needs progress. A step small enough to stay stable along the curvature-100 axis moves the curvature-1 axis by only about one percent per iteration, so the path bounces across the narrow valley and creeps along it.

open as a page

For f(x, y) = x^2 + 3xy at (1, 2), what is the directional derivative along (1, 1)/sqrt(2)?

level: middleimportance: should knowfreq 55%

basics

~20 s

The gradient of x^2 + 3xy is (2x + 3y, 3x), which is (8, 3) at (1, 2). Combining it with the unit direction gives (8 + 3)/sqrt(2) = 11/sqrt(2), about 7.78 per unit distance.

open as a page

Why is a nonzero gradient always perpendicular to the level curves of a function f(x, y)?

level: middleimportance: should knowfreq 48%

basics

~10 s

Along a level curve the function is constant, so the rate of change in the tangent direction is zero. With a nonzero gradient, that combination vanishes only if the two are at right angles.

open as a page

What does the numeric value of a Lagrange multiplier tell you about the optimum?

level: middleimportance: should knowfreq 44%

basics

~20 s

A Lagrange multiplier is a shadow price: the rate at which the best achievable objective value changes when the constraint is relaxed by one unit. Multiplier 5 means one more unit buys about 5 more objective units.

open as a page

What does Jensen's inequality say about a convex function of a weighted average?

level: seniorimportance: should knowfreq 36%

basics

~20 s

For a convex f and weights t_i >= 0 summing to 1, f(sum t_i x_i) <= sum t_i f(x_i): the function of the weighted average never exceeds the weighted average of the function values. For a concave f the inequality reverses.

open as a page

For the double well f(x) = x^4 - 4x^2, why does the starting point decide which minimum you reach?

level: seniorimportance: should knowfreq 50%

basics

~20 s

f(x) = x^4 - 4x^2 has minima at x = -sqrt(2) and x = +sqrt(2) with a local maximum at x = 0 between them. A downhill search never climbs that barrier, so the starting sign fixes the answer.

open as a page

showing 1–30 of 45