Minimize L2, L1, and quantile losses
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of statistical estimators and optimization principles for L2, L1, and tilted absolute (quantile) loss functions, including use of derivatives and subgradients, order-statistics, algorithmic complexity, and robustness considerations.
Part 1: Minimize Squared Deviations
Constraints
- 1 <= len(xs) <= 200000
- -10^12 <= xs[i] <= 10^12
- All xs[i] are finite real numbers.
- For non-integer answers, an absolute or relative error of 1e-9 is acceptable.
Examples
Input: ([1, 2, 3, 4],)
Expected Output: 2.5
Explanation: The mean is (1 + 2 + 3 + 4) / 4 = 2.5.
Input: ([42],)
Expected Output: 42.0
Explanation: With one element, the unique minimizer is that element.
Hints
- Differentiate sum((xi - theta)^2) with respect to theta and set the result to zero.
- After simplification, the answer depends only on the sum of the values and the number of values.
Part 2: Minimize Absolute Deviations
Constraints
- 1 <= len(xs) <= 200000
- -10^12 <= xs[i] <= 10^12
- All xs[i] are finite real numbers.
- Expected O(n) time is desired; randomized quickselect is acceptable.
Examples
Input: ([3, 1, 2],)
Expected Output: [2, 2]
Explanation: Sorted order is [1, 2, 3]. The unique median is 2.
Input: ([7],)
Expected Output: [7, 7]
Explanation: A single value is the only minimizer.
Hints
- The subgradient of sum(|xi - theta|) depends on how many points are less than, equal to, and greater than theta.
- The endpoints of the minimizer interval are the two middle order statistics: indices (n - 1) // 2 and n // 2 in sorted order.
Part 3: Minimize Tilted Absolute Loss for the 90th Percentile
Constraints
- 1 <= len(xs) <= 200000
- -10^12 <= xs[i] <= 10^12
- All xs[i] are finite real numbers.
- Use tau = 9/10 exactly for rank calculations.
- Expected O(n) time is desired; randomized quickselect is acceptable.
Examples
Input: ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],)
Expected Output: [9, 10]
Explanation: Here 0.9 * n = 9 exactly, so the loss is flat between the 9th and 10th sorted values.
Input: ([5],)
Expected Output: [5, 5]
Explanation: With one value, the only possible 90th-percentile minimizer is that value.
Hints
- At a minimizer theta, the counts must satisfy: number of values less than theta <= 0.9n <= number of values less than or equal to theta.
- In 1-based sorted order, the minimizer interval is [x_(ceil(0.9n)), x_(floor(0.9n) + 1)]. Use integer arithmetic with 9n / 10.