Reported September 2026
Two Sigmamath

Daily Temperature By Town

Reported by candidates from Two Sigma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Two Sigma OA. Under 2s to a working solution.
Founder's read

Two Sigma reported this one in September 2026, and it's labeled SQL, but it's really a regression problem wearing a query costume. Seven temperature columns, six output rows, and one row that wants a median of NYC where Town2 sits between 90 and 100. If you see OLS, greedy forward selection and pair search in a database question, don't panic. The table has at most 1000 rows and the structure is fixed. StealthCoder is the safety net if you blank mid-assessment, but the plan below gets you most of the way before you need it.

The problem

For this exercise, assume the input contains daily temperatures for NYC and six predictor towns named Town1 through Town6. Produce exactly six analysis rows.
Among NYC and all six towns, find the place with the largest population standard deviation of temperature.
Take the median NYC temperature among rows where Town2 is between 90 and 100, inclusive. Use 0 when there are no such rows, then round to the nearest integer.
For each town independently, fit ordinary least squares with an intercept to predict NYC from that town. Sum the absolute values of the six slope coefficients, excluding intercepts, then round to the nearest integer.
Find the single town whose intercept-based model has the smallest training mean squared error.
Find the pair of towns whose joint intercept-based model has the smallest training mean squared error.
Select five towns by forward greedy selection. Starting with no towns, repeatedly add the unused town that gives the smallest training mean squared error when fit jointly with the already selected towns and an intercept.
For every exact score tie, choose the lexicographically smaller place name or comma-joined town list. Round halfway values away from zero. All supplied predictor matrices have a unique least-squares solution.
Return columns metric, towns, and value in the exact row order above. Use null for towns on the two numeric-only rows and for value on the maximum-standard-deviation row. Pair names are comma-joined in lexicographic order; the five-town list is comma-joined in greedy selection order. The value for the last three selection rows is that model's training mean squared error.

Tables
temperatures: day_id PK (Integer), NYC (Decimal), Town1 (Decimal), Town2 (Decimal), Town3 (Decimal), Town4 (Decimal), Town5 (Decimal), Town6 (Decimal)

Constraints
The temperatures table has between 8 and 1000 rows.
day_id values are distinct; input row order does not affect the result.
Every temperature is a finite decimal with absolute value at most 10^6.
NYC and all six town columns contain no missing values.
Every tested predictor matrix, including the intercept column, has a unique least-squares solution.
Decimal results are compared with absolute tolerance 10^-6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that none of this needs a generic regression engine. Single-town OLS has a closed form: slope = cov(NYC, Town) / var(Town), and intercept follows from the means. Training MSE is the mean of squared residuals. Pairs need a 2-variable normal equation, which is a small 3x3 solve. Greedy selection grows that to up to 6 variables, so you need a general least-squares solve on the selected columns, or Gram-Schmidt style residual updates. The pitfalls are real. Population standard deviation divides by n, not n-1. The median needs the average of the two middle values when the count is even, and returns 0 when empty. Ties go to the lexicographically smaller name or list, and rounding is half away from zero. The row order is fixed, so use UNION ALL with explicit ordering. If the math or the SQL stalls live, StealthCoder is the hedge on the OA screen.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Daily Temperature By Town 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Two Sigma's OA.

Two Sigma reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Daily Temperature By Town FAQ

How hard is this Two Sigma SQL question really?+

Harder than the label suggests. Each metric is simple alone, but the greedy selection and joint least squares push past what plain SQL does comfortably. Expect to use recursive CTEs or closed-form normal equations. The difficulty is volume and precision, not any single clever idea.

What's the trick for the slope sum?+

Each town gets its own simple regression, so the slope is covariance of NYC and the town divided by the town's variance. Compute it with AVG of products minus product of AVGs. Take the absolute value of each of the six slopes, sum them, then round half away from zero.

How do I handle the median with the Town2 filter?+

Filter rows where Town2 is between 90 and 100 inclusive, order NYC, and take the middle one or the average of the two middle values. If no rows match, return 0. Round after that, using half away from zero, not banker's rounding.

How do ties get resolved?+

Every exact score tie goes to the lexicographically smaller place name or comma-joined town list. Add the name as a secondary ORDER BY after the score. For pairs, keep names sorted inside the pair so the joined string is already in lexicographic order.

How do I prepare in 48 hours?+

Practice closed-form simple regression in SQL, window-based medians, and UNION ALL with fixed row order. Then rehearse the 2-variable normal equation by hand. Don't try to build a general solver in SQL first. Get the first three rows right, then work the model-selection rows.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Two Sigma.

OA at Two Sigma?
Invisible during screen share
Get it