Regression Hedge
Reported by candidates from Millennium's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Millennium OA reported in September 2026 is labeled SQL, but the real work is a tiny linear algebra problem hiding in two tables. The thing it hinges on is the 2x2 normal-equation system built from de-meaned columns. You need hedge quantities for a target position using two hedge assets, via ordinary least squares. If you're taking this in the next day or two, don't hunt for a fancy window trick. It's aggregates plus a closed-form solve. StealthCoder sits invisible on your screen as a safety net if the algebra slips mid-assessment.
The problem
A portfolio holds target_quantity units of a target asset and can hedge with two other assets. Estimate a regression hedge from the supplied return history. Calculation Sort returns by period. Convert the three return columns to IEEE-754 float64. De-mean each return series independently. Solve the ordinary least-squares system X beta = y, where y is the de-meaned target return and the two columns of X are the de-meaned hedge returns. Return hedge quantities -target_quantity * beta in hedge-column order. Tables returns: period PK (Integer), target_return (Decimal), hedge_1_return (Decimal), hedge_2_return (Decimal) positions: config_id PK (Integer), target_quantity (Decimal) Constraints The return history contains between 3 and 2000 rows with unique periods. Every return and the target quantity is a finite exact decimal in [-1000, 1000]. After de-meaning, the two hedge-return columns have full column rank. positions contains exactly one row with config_id = 1. Numeric results are compared with absolute tolerance 0.000001.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: with only two regressors, you don't need a matrix library. De-mean each column by subtracting its average (AVG over the whole table, window or CTE). Then compute the sums Sxx = sum(x1*x1), Syy-style terms S12 = sum(x1*x2), S22 = sum(x2*x2), S1y = sum(x1*y), S2y = sum(x2*y). Solve the normal equations with Cramer's rule: det = S11*S22 - S12^2, beta1 = (S22*S1y - S12*S2y)/det, beta2 = (S11*S2y - S12*S1y)/det. Then output -target_quantity * beta1 and -target_quantity * beta2, joined to positions where config_id = 1. Pitfalls: integer or decimal division, forgetting to cast to float, and de-meaning after multiplying instead of before. Sorting by period doesn't change the sums, so don't sweat it. If you blank on the algebra during the live OA, StealthCoder is the hedge that hands you the structure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Regression Hedge cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Millennium's OA.
Millennium reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Regression Hedge FAQ
How hard is the Millennium Regression Hedge question really?+
The SQL is easy. The math is the hurdle. You need aggregates, a CTE for the means, and a closed-form 2x2 solve. If you remember the normal equations, it's about twenty lines. If you don't, it feels impossible.
What's the trick to solving it in SQL?+
Skip matrix inversion. De-mean each column in a CTE, sum the cross products, then apply Cramer's rule for the two betas. Multiply each by negative target_quantity and return them in hedge-column order.
Do I need to worry about float precision?+
Yes. The problem says convert to float64 and compares with tolerance 0.000001. Cast the decimal columns to a double type before computing means and sums, so you avoid decimal rounding or integer division surprises.
How do I get the target quantity into the calculation?+
Cross join the final betas to the positions table filtered on config_id = 1. It has exactly one row, so the join won't multiply rows. Then compute -target_quantity * beta for each hedge.
How should I prepare in 48 hours?+
Rehearse the de-mean CTE pattern and the 2x2 normal equation formulas until you can write them cold. Practice AVG() OVER () and SUM of products. Check your output on a tiny hand-made dataset where you know the betas.