Problem · Array
Backpropagation Through Sort and Median
Learn this problemProblem statement
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
nis odd.- The median is unique.
More OpenAI problems
- Memory AllocatorPHONE SCREEN · Seen Jul 2026
- Message Event AggregationONSITE INTERVIEW · Seen Jul 2026
- Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · Seen Jun 2026
- Streaming Entropy, Part 1: Batch EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 2: Numerically Stable EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 3: Block-wise EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 4: Stable Streaming EntropyONSITE INTERVIEW · Seen Jun 2026
- Resumable List IteratorPHONE SCREEN · Seen Jun 2026