Backpropagation Through Sort and Median
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The OpenAI OA reported in July 2026 looks like ML math at first and turns out to be a sorting problem in disguise. You get x and an upstream gradient v, and you need dL/dx for f(x) = sort(x) / median(x). The input size matters here. Anything worse than roughly n log n is out, so you can't build a full Jacobian. n is odd and the median is unique, which keeps the derivative clean. If you blank on the chain rule mid-assessment, StealthCoder runs invisibly as a safety net for the live OA.
The problem
Consider the function f: R^n -> R^n that sorts an input vector in ascending order and divides every sorted element by the median of the input: def f(x): return np.sort(x) / np.median(x) For example, f([3, 2, 1]) = [1/2, 1, 3/2]. Gradient task You are given the input vector x and an upstream gradient v = dL/dy, where y = f(x). Compute and return dL/dx. A runtime that is roughly linear in n, allowing logarithmic factors such as sorting, is acceptable. Function backpropSortMedian(x: double[], v: double[]) → double[] Examples Example 1 x = [3.0,2.0,1.0] v = [1.0,1.0,1.0] return = [0.5,-1.0,0.5] The direct contribution through sorting is 1 / 2 for each original element. The median element 2 also receives the denominator contribution -(1 + 2 + 3) / 2^2 = -3/2, so its total gradient is -1. Constraints n is odd. The median is unique.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Split the gradient into two parts. Let m be the median and s the sorted x. Output y_i = s_i / m. First part: the sorted element at rank i came from original index p_i, so x[p_i] gets v_i / m. That's a scatter through the argsort permutation. Second part: the median depends on x, and only the element at the median rank has derivative 1 with respect to m. dL/dm = sum over i of v_i * (-s_i / m^2). Add that scalar to the gradient of the original index holding the median. Check it against example 1: the median element 2 gets 1/2 plus -(6)/4 = -1. The pitfall is forgetting the denominator term, or applying it to every element instead of just the median index. Argsort once, then do everything in O(n log n). If the chain rule slips away live, StealthCoder is your hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Backpropagation Through Sort and Median 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI 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.
Backpropagation Through Sort and Median FAQ
What's the trick in the OpenAI backprop sort and median problem?+
Treat sort as a permutation. The gradient through sorting is just v_i / median scattered back to the original indices. Then add one extra scalar, the median's derivative, to the single index that holds the median. No Jacobian needed.
Why can't I build the full Jacobian?+
It's n by n, which is quadratic in memory and time. The problem says roughly linear with log factors is acceptable. Sorting once and doing a scatter plus one sum fits that budget easily.
Why does the problem say n is odd and the median is unique?+
It removes ambiguity. With odd n the median is a single element, so the derivative of the median with respect to x is 1 at exactly one index and 0 elsewhere. Even n would average two elements and split the gradient.
How do I verify my answer fast?+
Use example 1. x = [3,2,1], v = [1,1,1]. Each element gets 1/2 from the sort path. The median element 2 also gets -(1+2+3)/4 = -1.5, totaling -1. Expected output is [0.5, -1.0, 0.5].
How do I prepare in 48 hours for this kind of OpenAI question?+
Refresh the chain rule on simple ops like division, sort, and median. Practice writing argsort and scatter code in your language. Test on tiny hand-computed vectors. The code is short, so the risk is the math, not the implementation.