Implement Sampling and Minimize Loss in Numerical Coding
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates proficiency in probability and numerical methods, focusing on sampling from truncated distributions and analytical properties of estimators under various loss functions.
Constraints
- 1 <= n <= 200000
- |xi| <= 1e9
- mode in {'L2','L1','quantile'}
- For 'quantile' mode: 0 < tau <= 1
- Return the smallest minimizer if multiple minimizers exist
Hints
- For L2, set derivative of Σ(x−θ)^2 to zero to get θ = mean(X).
- For L1, any median minimizes the sum of absolute deviations; choose the lower median for determinism.
- For the τ-quantile, use k = ceil(τ*n) - 1 on the sorted order; compute k-th order statistic via Quickselect to avoid full sorting.