TICKETS 03 OF 3 RUNS LEFTACC --
OUTCRY

← Guide BookPlay it

Gradient Lab

Lagrange Optimizer

Maximize xy on a budget line ax + by = k by actually solving the Lagrange condition ∇f = λ∇g for x* - no readout to chase, just the algebra.

How it works

Each round generates a fresh problem: maximize f(x, y) = xy subject to ax + by = k with x, y ≥ 0, where a and b are random integers from 1 to 4 and k is a random integer from 6 to 21. You are asked for x* at the optimum and must type the number - there is no live objective readout to converge on visually.

The hint on screen sets up the method: ∇f = (y, x) and ∇g = (a, b), so the Lagrange condition ∇f = λ∇g gives y = λa and x = λb. Substituting both into the constraint yields a(λb) + b(λa) = k, so λ = k/(2ab), and then x* = λb and y* = λa.

After you check, the reveal walks the full solution: λ, x*, y*, a numerical check that ax* + by* ≈ k, and the maximum value of xy. Press next round for a new (a, b, k).

How scoring works

Your typed x* is graded within a tolerance of max(0.01, 1% of x*). A correct answer adds 1 to score; every checked answer adds 1 to rounds. There are no partial points and no timer.

The geometry: gradients must align on the constraint

At a constrained optimum, the level curve of the objective is tangent to the constraint set - if it crossed the constraint, you could slide along the constraint and improve. Tangency means the normals line up: ∇f = λ∇g. The multiplier λ is the proportionality factor, and economically it is the shadow price - the rate at which the optimal value grows per unit of extra k.

For this game's family, ∇f = (y, x) points perpendicular to the hyperbola xy = constant, and ∇g = (a, b) is the fixed normal of the budget line. The condition (y, x) = λ(a, b) says the optimal point is where a hyperbola just kisses the line. Everything after that is substitution.

Notice the cross-symmetry in the result: x* = λb and y* = λa - x* picks up b, the coefficient on the other variable. Working through why (y matches a because ∂f/∂x = y sits opposite the a-slot) inoculates you against the most common slip in this game.

The closed form and the equal-split interpretation

Carry the algebra to the end once and you never redo it: λ = k/(2ab), x* = λb = k/(2a), y* = λa = k/(2b). So the optimum spends exactly half the budget on each term: ax* = k/2 and by* = k/2. For maximizing a product under a linear budget, splitting the budget equally across the terms is the general pattern - the same result AM-GM gives.

That gives you a five-second solve for any round: x* = k/(2a). If a = 3 and k = 18, x* = 3. The maximum value is x*·y* = k²/(4ab) if you want the sanity check the reveal shows.

The AM-GM route is worth knowing as the cross-check: with u = ax and v = by, u + v = k fixed, and xy = uv/(ab) is maximized when u = v = k/2. Two independent methods landing on the same number is exactly the kind of verification interviewers like to see.

Where Lagrange multipliers earn their keep

The method generalizes far beyond this toy: any smooth objective with equality constraints yields stationarity of the Lagrangian L = f - λ(g - k). In quant work this is portfolio optimization (maximize expected return subject to a risk budget and full investment - the classic mean-variance solution is a Lagrange computation), utility maximization under a budget, and maximum entropy distributions under moment constraints.

The multiplier itself is often the deliverable: λ = ∂(optimal value)/∂k tells you what one more unit of budget or risk is worth. In this game, the optimal value is k²/(4ab), and differentiating with respect to k gives k/(2ab) - exactly the λ you computed. That identity is a favorite follow-up question.

Also keep the boundary caveat in mind: Lagrange finds interior tangency points. Here the constraint x, y ≥ 0 is slack at the optimum (both come out strictly positive), so the tangency point is the answer - but in general you must check corner solutions too.

A worked example

Round: maximize f(x, y) = xy subject to 2x + 3y = 12, x, y ≥ 0. What is x*?

Step 1 - Lagrange condition. ∇f = (y, x), ∇g = (2, 3). Setting (y, x) = λ(2, 3): y = 2λ and x = 3λ.

Step 2 - substitute into the constraint. 2(3λ) + 3(2λ) = 12λ = 12, so λ = 1. In general λ = k/(2ab) = 12/(2·2·3) = 1 - same thing.

Step 3 - read off the optimum. x* = λb = 3, y* = λa = 2. Answer: 3. Tolerance is 1% of 3, so 2.97 to 3.03 passes.

Step 4 - verify. Budget: 2·3 + 3·2 = 12, exactly k, split evenly (6 and 6). Value: xy = 6 = k²/(4ab) = 144/24. Nudge test: (x, y) = (3.3, 1.8) also satisfies the budget but gives 5.94 < 6 - the tangency point wins.

Common mistakes

Swapping the coefficients: writing x* = λa instead of x* = λb. Because ∇f = (y, x) crosses the variables, x pairs with b - the equal-split form x* = k/(2a) is the safest way to remember it.

Setting ∇f = ∇g without λ - solving y = a, x = b. That point almost never lies on the constraint; the multiplier is what scales the gradient direction to reach it.

Answering the maximum value of xy, or y*, when the question asks for x*. Read what is asked before typing.

Splitting x and y equally instead of splitting the budget equally. The optimum has ax* = by* = k/2; x* = y* only when a = b.

Ignoring the check the reveal offers: if a·(your x*) is not close to k/2, you slipped somewhere. Verification against the constraint is free and catches sign and swap errors instantly.

Why interviews test this

Constrained optimization is the backbone of the quant-research canon: mean-variance portfolios, maximum likelihood under constraints, and utility maximization are all Lagrange computations, and interviews regularly ask a small closed-form instance exactly like this game's - maximize a product or a log-sum under a linear budget.

The two follow-ups to prepare: interpret λ as a shadow price (the derivative of the optimal value with respect to the constraint level), and reproduce the answer by a second route such as AM-GM or direct substitution of y = (k - ax)/b followed by one-variable calculus. Interviewers use the second route to separate memorized formulas from understanding.

Play Lagrange Optimizer · All game guides · The arcade